2016-11-29 2 views
0

갤러리에서 이미지를 선택하고 이미지보기로 설정할 때가 있습니다. 회전됩니다. 나는 그것의 기본 방향으로 원한다.Android에서 갤러리에서 선택한 기본 방향으로 이미지를 회전하는 방법은 무엇입니까?

+0

SaveCompressImage mSaveCompressImage = new SaveCompressImage(getApplicationContext()); String path = mSaveCompressImage.compressImage(imageFilePath); 

을 상세하게 설명한다. 당신은 지금까지 무엇을 했습니까 !! – AndiGeeky

+0

코드 작성 방법을 보여주는 코드를 여기에 추가하십시오! – AndiGeeky

+0

나는 그것을위한 해결책을 찾았고 나는 다른 사람들과 그것을 공유하고 싶어했다 ... 나의 질문에 대한 대답으로 –

답변

2

갤러리에서 선택한 이미지 회전을 해결하기 위해 (이 코드는 두 가지 다른 소스의 조합입니다.) 나를 올바르게 작동 시켰습니다.

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == PICK_IMAGE_REQUEST && data != null && data.getData() != null) { 

     Uri uri = data.getData(); 

     try { 
       //..First convert the Image to the allowable size so app do not throw Memory_Out_Bound Exception 
       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inJustDecodeBounds = true; 
        BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options); 
       int resolution = 500; 
        options.inSampleSize = calculateInSampleSize(options, resolution , resolution); 
        options.inJustDecodeBounds = false; 
        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options); 

       //...Now You have the 'bitmap' to rotate.... 
       //...Rotate the bitmap to its original Orientation... 
       Bitmap bitmapNew = ImageOrientation.modifyOrientation(getApplicationContext(),bitmap,uri); 

       //...After Rotation set the image to Image View... 
        imageViewProfile.setImageBitmap(bitmapNew); 


     } catch (Exception e) { 
      Log.d("Image_exception",e.toString()); 
     } 
    } 

} 
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) >= reqHeight 
       && (halfWidth/inSampleSize) >= reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 

ImageOrientation 클래스 is..You의 소스 코드는 당신이 필요로하는 적 활동이나 곳에서만 modifyOrientation() 메서드를 호출해야합니다. 갤러리/카메라에서 선택한 후 올바른 orientaion을 얻기를위한

public class ImageOrientation { 


    public static Bitmap modifyOrientation(Context context,Bitmap bitmap,Uri uri) throws IOException { 
     ExifInterface ei = new ExifInterface(getPath(context,uri)); 
     int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

     switch (orientation) { 
      case ExifInterface.ORIENTATION_ROTATE_90: 
       return rotate(bitmap, 90); 

      case ExifInterface.ORIENTATION_ROTATE_180: 
       return rotate(bitmap, 180); 

      case ExifInterface.ORIENTATION_ROTATE_270: 
       return rotate(bitmap, 270); 

      case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: 
       return flip(bitmap, true, false); 

      case ExifInterface.ORIENTATION_FLIP_VERTICAL: 
       return flip(bitmap, false, true); 

      default: 
       return bitmap; 
     } 
    } 

    private static String getPath(final Context context, final Uri uri) { 

     final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; 

     // DocumentProvider 
     if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { 
      // ExternalStorageProvider 
      if (isExternalStorageDocument(uri)) { 
       final String docId = DocumentsContract.getDocumentId(uri); 
       final String[] split = docId.split(":"); 
       final String type = split[0]; 

       if ("primary".equalsIgnoreCase(type)) { 
        return Environment.getExternalStorageDirectory() + "/" + split[1]; 
       } 

       // TODO handle non-primary volumes 
      } 
      // DownloadsProvider 
      else if (isDownloadsDocument(uri)) { 

       final String id = DocumentsContract.getDocumentId(uri); 
       final Uri contentUri = ContentUris.withAppendedId(
         Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); 

       return getDataColumn(context, contentUri, null, null); 
      } 
      // MediaProvider 
      else if (isMediaDocument(uri)) { 
       final String docId = DocumentsContract.getDocumentId(uri); 
       final String[] split = docId.split(":"); 
       final String type = split[0]; 

       Uri contentUri = null; 
       if ("image".equals(type)) { 
        contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
       } else if ("video".equals(type)) { 
        contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; 
       } else if ("audio".equals(type)) { 
        contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
       } 

       final String selection = "_id=?"; 
       final String[] selectionArgs = new String[] { 
         split[1] 
       }; 

       return getDataColumn(context, contentUri, selection, selectionArgs); 
      } 
     } 
     // MediaStore (and general) 
     else if ("content".equalsIgnoreCase(uri.getScheme())) { 
      return getDataColumn(context, uri, null, null); 
     } 
     // File 
     else if ("file".equalsIgnoreCase(uri.getScheme())) { 
      return uri.getPath(); 
     } 

     return null; 
    } 

    /** 
    * Get the value of the data column for this Uri. This is useful for 
    * MediaStore Uris, and other file-based ContentProviders. 
    * 
    * @param context The context. 
    * @param uri The Uri to query. 
    * @param selection (Optional) Filter used in the query. 
    * @param selectionArgs (Optional) Selection arguments used in the query. 
    * @return The value of the _data column, which is typically a file path. 
    */ 
    private static String getDataColumn(Context context, Uri uri, String selection, 
             String[] selectionArgs) { 

     Cursor cursor = null; 
     final String column = "_data"; 
     final String[] projection = { 
       column 
     }; 

     try { 
      cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, 
        null); 
      if (cursor != null && cursor.moveToFirst()) { 
       final int column_index = cursor.getColumnIndexOrThrow(column); 
       return cursor.getString(column_index); 
      } 
     } finally { 
      if (cursor != null) 
       cursor.close(); 
     } 
     return null; 
    } 


    /** 
    * @param uri The Uri to check. 
    * @return Whether the Uri authority is ExternalStorageProvider. 
    */ 
    private static boolean isExternalStorageDocument(Uri uri) { 
     return "com.android.externalstorage.documents".equals(uri.getAuthority()); 
    } 

    /** 
    * @param uri The Uri to check. 
    * @return Whether the Uri authority is DownloadsProvider. 
    */ 
    private static boolean isDownloadsDocument(Uri uri) { 
     return "com.android.providers.downloads.documents".equals(uri.getAuthority()); 
    } 

    /** 
    * @param uri The Uri to check. 
    * @return Whether the Uri authority is MediaProvider. 
    */ 
    private static boolean isMediaDocument(Uri uri) { 
     return "com.android.providers.media.documents".equals(uri.getAuthority()); 
    } 



    private static Bitmap rotate(Bitmap bitmap, float degrees) { 
     Matrix matrix = new Matrix(); 
     matrix.postRotate(degrees); 
     return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
    } 

    private static Bitmap flip(Bitmap bitmap, boolean horizontal, boolean vertical) { 
     Matrix matrix = new Matrix(); 
     matrix.preScale(horizontal ? -1 : 1, vertical ? -1 : 1); 
     return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
    } 


} 
+0

해결책을 찾았습니다 !! 큰! – AndiGeeky

+0

다른 사람이 그것을 얻을 수 있도록이 솔루션을 시도하고 도움으로 표를하십시오 –

1

: 갤러리에서 선택하면

ExifInterface exifInterface = new ExifInterface(photoPath); 
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
     ExifInterface.ORIENTATION_UNDEFINED); 

switch(orientation) { 
    case ExifInterface.ORIENTATION_ROTATE_90: 
     rotateImage(bitmap, 90); 
     break; 
    case ExifInterface.ORIENTATION_ROTATE_180: 
     rotateImage(bitmap, 180); 
     break; 
    case ExifInterface.ORIENTATION_ROTATE_270: 
     rotateImage(bitmap, 270); 
     break; 
    case ExifInterface.ORIENTATION_NORMAL: 
    default: 
     break; 
} 


public Bitmap rotateImage(Bitmap source, float angle) { 
    Matrix mat = new Matrix(); 
    mat.postRotate(angle); 

    Bitmap bitmap= Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), mat, 

    return bitmap; 
} 
+0

이 각도가 필요합니다 ... 하지만 해결책은 내가 기본값으로 그림을 설정하고 이미지보기에서 설정하는 전체 코드입니다 –

+0

야간 코더 , 그는 단지 내가 올바른 방향으로 이미지를 가져와야한다고 대답했습니다. 나는 왜 당신이 여기에 onActivityResult 메소드를 넣는 지 모르겠다. 또한, 코드를 자동으로 감지 할 각도를 필요로하지 않는다. 당신의 코멘트로 무엇을 말하고 싶니? –

+0

Arun Kumar, 철저하지 마십시오. 나는 당신의 대답이 틀렸다고 말하지 않았다! –

0

이미지 회전을 해결하기 위해, 나는 이것을 사용하고 그것은 나를 위해 작동합니다. SaveCompressImage.java

public class SaveCompressImage { 
    Context context; 
    public SaveCompressImage(Context context){ 
     this.context=context; 
    } 

    public String compressImage(String imageUri) { 
     String filename=null ; 
     try { 
      String filePath = getRealPathFromURI(imageUri) ; 
      Bitmap scaledBitmap = null; 

      BitmapFactory.Options options = new BitmapFactory.Options(); 

      // by setting this field as true, the actual bitmap pixels are not 
      // loaded in the memory. Just the bounds are loaded. If 
      // you try the use the bitmap here, you will get null. 
      options.inJustDecodeBounds = true; 

      Bitmap bmp = BitmapFactory.decodeFile(filePath, options); 


      int actualHeight = options.outHeight; 
      int actualWidth = options.outWidth; 

      // max Height and width values of the compressed image is taken as 
      // 816x612 

      float maxHeight = 816.0f; 
      float maxWidth = 612.0f; 
      float imgRatio = (float)actualWidth/actualHeight; 
      float maxRatio = maxWidth/maxHeight; 

      // width and height values are set maintaining the aspect ratio of the 
      // image 

      if (actualHeight > maxHeight || actualWidth > maxWidth) { 
       if (imgRatio < maxRatio) { 
        imgRatio = maxHeight/actualHeight; 
        actualWidth = (int) (imgRatio * actualWidth); 
        actualHeight = (int) maxHeight; 
       } else if (imgRatio > maxRatio) { 
        imgRatio = maxWidth/actualWidth; 
        actualHeight = (int) (imgRatio * actualHeight); 
        actualWidth = (int) maxWidth; 
       } else { 
        actualHeight = (int) maxHeight; 
        actualWidth = (int) maxWidth; 

       } 
      } 

      // setting inSampleSize value allows to load a scaled down version of 
      // the original image 

      options.inSampleSize = calculateInSampleSize(options, actualWidth, 
        actualHeight); 

      // inJustDecodeBounds set to false to load the actual bitmap 
      options.inJustDecodeBounds = false; 

      // this options allow android to claim the bitmap memory if it runs low 
      // on memory 
      options.inPurgeable = true; 
      options.inInputShareable = true; 
      options.inTempStorage = new byte[16 * 1024]; 

      try { 
       // load the bitmap from its path 
       bmp =BitmapFactory.decodeFile(filePath, options); 
      } catch (OutOfMemoryError exception) { 

      } 
      try { 
       scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, 
         Bitmap.Config.ARGB_8888); 
      } catch (OutOfMemoryError exception) { 
      } 
      if(scaledBitmap!=null){ 
       float ratioX = actualWidth/(float) options.outWidth; 
       float ratioY = actualHeight/(float) options.outHeight; 
       float middleX = actualWidth/2.0f; 
       float middleY = actualHeight/2.0f; 

       Matrix scaleMatrix = new Matrix(); 
       scaleMatrix.setScale(ratioX, ratioY, middleX, middleY); 

       Canvas canvas = new Canvas(scaledBitmap); 
       canvas.setMatrix(scaleMatrix); 
       canvas.drawBitmap(bmp, middleX - bmp.getWidth()/2, 
         middleY - bmp.getHeight()/2, new Paint(
           Paint.FILTER_BITMAP_FLAG)); 

       // check the rotation of the image and display it properly 
       ExifInterface exif; 
       try { 
        exif = new ExifInterface(filePath); 

        int orientation = exif.getAttributeInt(
          ExifInterface.TAG_ORIENTATION, 0); 
        Log.d("EXIF", "Exif: " + orientation); 
        Matrix matrix = new Matrix(); 
        if (orientation == 6) { 
         matrix.postRotate(90); 
         Log.d("EXIF", "Exif: " + orientation); 
        } else if (orientation == 3) { 
         matrix.postRotate(180); 
         Log.d("EXIF", "Exif: " + orientation); 
        } else if (orientation == 8) { 
         matrix.postRotate(270); 
         Log.d("EXIF", "Exif: " + orientation); 
        } 
        scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, 
          scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, 
          true); 
       } catch (IOException ioExceptione) { 

       } 
       FileOutputStream out = null; 
       filename = getFilename(); 
       try { 
        out = new FileOutputStream(filename); 
        // write the compressed bitmap at the destination specified by 
        // filename. 
        scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); 
       } catch (FileNotFoundException fileNotFoundException) { 

       } 
      } 
     } catch (Exception exception) { 

     } 
     return filename; 
    } 

    public String getFilename() { 
     File file = new File(Constants.IMAGE_STORE_FOLDER); 
     if (!file.exists()) { 
      file.mkdirs(); 
     } 
     String uriSting = (Constants.IMAGE_STORE_FOLDER 
       + System.currentTimeMillis() + ".jpg"); 
     return uriSting; 
    } 

    public int calculateInSampleSize(BitmapFactory.Options options, 
      int reqWidth, int reqHeight) { 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 
      final int heightRatio = Math.round((float) height 
        /(float) reqHeight); 
      final int widthRatio = Math.round((float) width/(float) reqWidth); 
      inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
     } 
     final float totalPixels = (float)width * height; 
     final float totalReqPixelsCap = (float)reqWidth * reqHeight * 2; 
     while (totalPixels/(inSampleSize * inSampleSize) > totalReqPixelsCap) { 
      inSampleSize++; 
     } 
     return inSampleSize; 
    } 

    /** 
    * Get a file path from a Uri. This will get the the path for Storage Access 
    * Framework Documents, as well as the _data field for the MediaStore and 
    * other file-based ContentProviders. 
    * 
    * @param context The context. 
    * @param uri The Uri to query. 
    * @author paulburke 
    */ 

    public static String getPath(final Context context, final Uri uri) { 

     final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; 
     boolean a=DocumentsContract.isDocumentUri(context, uri); 
     // DocumentProvider 
     if (isKitKat) { 
      // ExternalStorageProvider 
      if (isExternalStorageDocument(uri)) { 
       final String docId = DocumentsContract.getDocumentId(uri); 
       final String[] split = docId.split(":"); 
       final String type = split[0]; 

       if ("primary".equalsIgnoreCase(type)) { 
        return Environment.getExternalStorageDirectory() + "/" + split[1]; 
       } 
       // handle non-primary volumes 
      } 
      // DownloadsProvider 
      else if (isDownloadsDocument(uri)) { 

       final String id = DocumentsContract.getDocumentId(uri); 
       final Uri contentUri = ContentUris.withAppendedId(
         Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); 

       return getDataColumn(context, contentUri, null, null); 
      } 
      // MediaProvider 
      else if (isMediaDocument(uri)) { 
       final String docId = DocumentsContract.getDocumentId(uri); 
       final String[] split = docId.split(":"); 
       final String type = split[0]; 

       Uri contentUri = null; 
       if ("image".equals(type)) { 
        contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
       } else if ("video".equals(type)) { 
        contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; 
       } else if ("audio".equals(type)) { 
        contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
       } 

       final String selection = "_id=?"; 
       final String[] selectionArgs = new String[] { 
         split[1] 
       }; 

       return getDataColumn(context, contentUri, selection, selectionArgs); 
      } 
     } 
     // MediaStore (and general) 
     else if ("content".equalsIgnoreCase(uri.getScheme())) { 

      // Return the remote address 
      if (isGooglePhotosUri(uri)) 
       return uri.getLastPathSegment(); 

      return getDataColumn(context, uri, null, null); 
     } 
     // File 
     else if ("file".equalsIgnoreCase(uri.getScheme())) { 
      return uri.getPath(); 
     } 

     return null; 
    } 

    /** 
    * Get the value of the data column for this Uri. This is useful for 
    * MediaStore Uris, and other file-based ContentProviders. 
    * 
    * @param context The context. 
    * @param uri The Uri to query. 
    * @param selection (Optional) Filter used in the query. 
    * @param selectionArgs (Optional) Selection arguments used in the query. 
    * @return The value of the _data column, which is typically a file path. 
    */ 
    public static String getDataColumn(Context context, Uri uri, String selection, 
      String[] selectionArgs) { 

     Cursor cursor = null; 
     final String column = "_data"; 
     final String[] projection = { 
       column 
     }; 

     try { 
      cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, 
        null); 
      if (cursor != null && cursor.moveToFirst()) { 
       final int index = cursor.getColumnIndexOrThrow(column); 
       return cursor.getString(index); 
      } 
     } finally { 
      if (cursor != null) 
       cursor.close(); 
     } 
     return null; 
    } 


    /** 
    * @param uri The Uri to check. 
    * @return Whether the Uri authority is ExternalStorageProvider. 
    */ 
    public static boolean isExternalStorageDocument(Uri uri) { 
     return "com.android.externalstorage.documents".equals(uri.getAuthority()); 
    } 

    /** 
    * @param uri The Uri to check. 
    * @return Whether the Uri authority is DownloadsProvider. 
    */ 
    public static boolean isDownloadsDocument(Uri uri) { 
     return "com.android.providers.downloads.documents".equals(uri.getAuthority()); 
    } 

    /** 
    * @param uri The Uri to check. 
    * @return Whether the Uri authority is MediaProvider. 
    */ 
    public static boolean isMediaDocument(Uri uri) { 
     return "com.android.providers.media.documents".equals(uri.getAuthority()); 
    } 

    /** 
    * @param uri The Uri to check. 
    * @return Whether the Uri authority is Google Photos. 
    */ 
    public static boolean isGooglePhotosUri(Uri uri) { 
     return "com.google.android.apps.photos.content".equals(uri.getAuthority()); 
    } 

    public String getRealPathFromURI(String contentURI) { 
     Uri contentUri = Uri.parse(contentURI); 
     Cursor cursor = context.getContentResolver().query(contentUri, 
       null, null, null, null); 
     if (cursor == null) { 
      return contentUri.getPath(); 
     } else { 
      cursor.moveToFirst(); 
      int index = cursor 
        .getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
      String pathh=cursor.getString(index); 
      return pathh; 
     } 
    } 
} 
관련 문제