2014-11-17 2 views
0

저는 카메라를 열고 사진을 찍는 버튼을 클릭하는 프로젝트에서 작업하고 있습니다. imageview.I 파일에 이미지를 저장하고 내 전화의 디렉토리에서 이미지를 열면 큰 크기로 표시됩니다. 이미지 뷰에서 이미지를 더 크게 표시하려면 어떻게해야합니까?이미지가 너비와 높이가 wrap_content인데도 불구하고 안드로이드에서 너무 작게 표시되었습니다.

코드 :

btncamera.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       captureImage(); 
      } 
     }); 

private void captureImage() { 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

    // start the image capture Intent 
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); 
} 

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // if the result is capturing Image 
     if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { 
      if (resultCode ==getActivity().RESULT_OK) { 
       // successfully captured the image 
       // display it in image view 
       //image_uri=data.getData().toString(); 
       previewCapturedImage(); 
      } else if (resultCode == getActivity().RESULT_CANCELED) { 
       // user cancelled Image capture 
       image_path=null; 
       Toast.makeText(getActivity(), 
         "User cancelled image capture", Toast.LENGTH_SHORT) 
         .show(); 
      } else { 
       // failed to capture image 
       Toast.makeText(getActivity(), 
         "Sorry! Failed to capture image", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     } 
    } 
    private void previewCapturedImage() { 
     try { 





      // bimatp factory 
      BitmapFactory.Options options = new BitmapFactory.Options(); 



       options.inSampleSize = 4; 

       final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),options); 
       ExifInterface exif = null; 
       try { 
        exif = new ExifInterface(fileUri.getPath()); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION); 
       int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL; 

       int rotationAngle = 0; 
       if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90; 
       if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180; 
       if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270; 

       Matrix matrix = new Matrix(); 
       matrix.setRotate(rotationAngle, (float) bitmap.getWidth()/2, (float) bitmap.getHeight()/2); 
       Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, options.outWidth, options.outHeight, matrix, true); 

      iv_preview.setImageBitmap(rotatedBitmap); 

     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } 
    } 

    public Uri getOutputMediaFileUri(int type) { 
     return Uri.fromFile(getOutputMediaFile(type)); 
    } 

    /* 
    * returning image/video 
    */ 
    private File getOutputMediaFile(int type) { 

     // External sdcard location 
     File mediaStorageDir = new File(
       Environment 
         .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
       IMAGE_DIRECTORY_NAME); 

     // Create the storage directory if it does not exist 
     if (!mediaStorageDir.exists()) { 
      if (!mediaStorageDir.mkdirs()) { 
       Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create " 
         + IMAGE_DIRECTORY_NAME + " directory"); 
       return null; 
      } 
     } 

     // Create a media file name 
//  String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
//    Locale.getDefault()).format(new Date()); 
     File mediaFile; 
     if (type == MEDIA_TYPE_IMAGE) { 

      //change image_path file name required for marina with the name of the file 
      mediaFile = new File(mediaStorageDir.getPath()+File.separator+user_id+"_"+job_object.job_id+"_"+task_id+".jpg"); 
//   if(mediaFile.exists()) 
//   { 
//    mediaFile.delete(); 
//    mediaFile = new File(mediaStorageDir.getPath()+File.separator+user_id+"_"+job_object.job_id+"_"+task_id+".jpg"); 
//   } 
      //IF YOU MAKE CHANGE TO THE FILE NAME PATH ALSO CHANGE IN loadpreviousimage method 
      image_path=mediaStorageDir.getPath() + File.separator+user_id+"_"+job_object.job_id+"_"+task_id+".jpg"; 
     } else { 
      return null; 
     } 

     return mediaFile; 
    } 

내가 작은 이미지를 얻고 있다고 때문에이 라인 options.inSampleSize = 4;의 그것입니까?

어떻게 문제를 해결할 수 있습니까?

+0

'options.inSampleSize = 4' 너무 당신의 높은 해상도 이미지를 디코딩하지만 이미지 품질이 좋은하지 않습니다. 그래서'options.inSampleSize = 1'을 사용할 수 있습니다 – Piyush

답변

0

사용 디코딩이 방법,

public Bitmap decodeFile(String path) { 
     try { 
      // Decode image size 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeFile(path, o); 
      // The new size we want to scale to 
      final int REQUIRED_SIZE = 70//put size you want; 

      // Find the correct scale value. It should be the power of 2. 
      int scale = 1; 
      while (o.outWidth/scale/2 >= REQUIRED_SIZE && o.outHeight/scale/2 >= REQUIRED_SIZE) 
       scale *= 2; 

      // Decode with inSampleSize 
      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize = scale; 
      return BitmapFactory.decodeFile(path, o2); 
     } catch (Throwable e) { 
      e.printStackTrace(); 
     } 
     return null; 

    } 

희망이 당신이

+0

int REQUIRED_SIZE 값은 무엇입니까?이 코드를 직접 사용해 보았습니까? –

+0

실제로 이미지 크기를 줄입니다 ... –

1

예, inSampleSize를가되는 것은> (1)이 서브 이미지를 샘플링 할 경우 도움이 될 것입니다; 꽤 큰 이미지를 축소하는 데 사용됩니다. 당신이 당신의 이미지에 맞게 이미지 뷰에

android:scaleType="fitXY"

를 사용할 수 Source here. 그리고, 당신의 ImageView

+0

문제는 내 이미지가 640x480이지만 특정 크기보다 크게 만들 수 없다는 것입니다. 그러나 위의 포스터는 더 아래로 확장하는 코드를 제공합니다. 내 문제는 이미지가 이미 작다는 것입니다. –

관련 문제