Skip to content Skip to sidebar Skip to footer

Capture Image Rotate After Upload

I am uploading image to server but image is rotate after uploaded to server Even preview is showing correct. So many people facing this problem i found this link but didn't work. A

Solution 1:

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

Solution 2:

Call this function where you get path of image

publicvoidsetImage(String _path) {
    intorientation= CustomImageUtil.getExifOrientation(_path);
    BitmapFactory.Optionsresample=newBitmapFactory.Options();
    resample.inSampleSize = 4;
    Bitmapbitmap= BitmapFactory.decodeFile(_path, resample);
    if (orientation == 90) {
        bitmap = CustomImageUtil.rotate(bitmap, 90);
    } elseif (orientation == 180) {
        bitmap = CustomImageUtil.rotate(bitmap, 180);
    } elseif (orientation == 270) {
        bitmap = CustomImageUtil.rotate(bitmap, 270);
    }

    // use your bitmap here


}

CustomImageUtil.class:

publicclassCustomImageUtil {



publicstatic  String getRealPathFromURI(Context context,Uri contentURI) {
    String result;
    Cursorcursor= 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(); 
        intidx= cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
        result = cursor.getString(idx);
        cursor.close();
    }
    return result;
}

// method for bitmap to base64publicstatic String encodeTobase64(Bitmap image) {
    Bitmapimmage= image;
    ByteArrayOutputStreambaos=newByteArrayOutputStream();
    immage.compress(Bitmap.CompressFormat.PNG, 60, baos);
    byte[] b = baos.toByteArray();
    StringimageEncoded= 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
 */publicstaticintgetExifOrientation(String filepath) {

    intdegree=0;
    ExifInterfaceexif=null;
    try {
        exif = newExifInterface(filepath);
    } catch (IOException ex) {ex.printStackTrace();
    }
    if (exif != null) {
        intorientation= 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.publicstatic Bitmap rotate(Bitmap b, int degrees) {
    if (degrees != 0 && b != null) {
        Matrixm=newMatrix();
        m.setRotate(degrees, (float) b.getWidth() / 2,
                (float) b.getHeight() / 2);
        try {
            Bitmapb2= 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) {
ByteArrayOutputStreambytes=newByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
Stringpath= Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
} 

Post a Comment for "Capture Image Rotate After Upload"