Capture image rotate after upload

▼魔方 西西 提交于 2019-12-02 12:09:59
Pedro Oliveira

You shouldn't rotate the image after upload. You need to rotate it before. The preview is correct maybe because you're respecting Exif values when showing it. But the server isn't.

You need to rotate the image according to it's exif rotation:

https://stackoverflow.com/a/20480741/3410697

And only then you should upload it to the server

Call this function where you get path of image

public void setImage(String _path) {
    int orientation = CustomImageUtil.getExifOrientation(_path);
    BitmapFactory.Options resample = new BitmapFactory.Options();
    resample.inSampleSize = 4;
    Bitmap bitmap = BitmapFactory.decodeFile(_path, resample);
    if (orientation == 90) {
        bitmap = CustomImageUtil.rotate(bitmap, 90);
    } else if (orientation == 180) {
        bitmap = CustomImageUtil.rotate(bitmap, 180);
    } else if (orientation == 270) {
        bitmap = CustomImageUtil.rotate(bitmap, 270);
    }

    // use your bitmap here


}

CustomImageUtil.class:

 public class CustomImageUtil {



public static  String getRealPathFromURI(Context context,Uri contentURI) {
    String result;
    Cursor cursor = context.getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) { // Source is Dropbox or other similar local file path
        result = contentURI.getPath();
    } else { 
        cursor.moveToFirst(); 
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
        result = cursor.getString(idx);
        cursor.close();
    }
    return result;
}

// method for bitmap to base64
public static String encodeTobase64(Bitmap image) {
    Bitmap immage = image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    immage.compress(Bitmap.CompressFormat.PNG, 60, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);

    Log.d("Image Log:", imageEncoded);
    return imageEncoded;



}

/**
 * getExifOrientation -- Roate the image on the right angel
 * @param filepath -- path of the file to be rotated
 * @return
 */
public static int getExifOrientation(String filepath) {

    int degree = 0;
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(filepath);
    } catch (IOException ex) {ex.printStackTrace();
    }
    if (exif != null) {
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION, -1);
        if (orientation != -1) {
            // We only recognize a subset of orientation tag values.
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }

        }
    }
    return degree;
}



// Rotates the bitmap by the specified degree.
// If a new bitmap is created, the original bitmap is recycled.
public static Bitmap rotate(Bitmap b, int degrees) {
    if (degrees != 0 && b != null) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) b.getWidth() / 2,
                (float) b.getHeight() / 2);
        try {
            Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
                    b.getHeight(), m, true);
            if (b != b2) {
                b.recycle();
                b = b2;
            }
        } catch (OutOfMemoryError ex) {ex.printStackTrace();
        }
    }
    return b;
}


 }

To convert Bitmap to Uri

public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
} 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!