Android Orientation Issue On Samsung Devies With Exif
There is a problem when taking photo using camera on samsung devices in the landscape (http://prntscr.com/fx29hs) Problem is that Exif always return the same value for both cases
Solution 1:
add this class
public class ImageUtils {
publicstatic ImageUtils mInstant;
publicstatic ImageUtils getInstant(){
if(mInstant==null){
mInstant = newImageUtils();
}
return mInstant;
}
public Bitmap getCompressedBitmap(String imagePath) {
floatmaxHeight=1920.0f;
floatmaxWidth=1080.0f;
BitmapscaledBitmap=null;
BitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmapbmp= BitmapFactory.decodeFile(imagePath, options);
intactualHeight= options.outHeight;
intactualWidth= options.outWidth;
floatimgRatio= (float) actualWidth / (float) actualHeight;
floatmaxRatio= maxWidth / maxHeight;
if (actualHeight > maxHeight || actualWidth > maxWidth) {
if (imgRatio < maxRatio) {
imgRatio = maxHeight / actualHeight;
actualWidth = (int) (imgRatio * actualWidth);
actualHeight = (int) maxHeight;
} elseif (imgRatio > maxRatio) {
imgRatio = maxWidth / actualWidth;
actualHeight = (int) (imgRatio * actualHeight);
actualWidth = (int) maxWidth;
} else {
actualHeight = (int) maxHeight;
actualWidth = (int) maxWidth;
}
}
options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
options.inJustDecodeBounds = false;
options.inDither = false;
options.inPurgeable = true;
options.inInputShareable = true;
options.inTempStorage = newbyte[16 * 1024];
try {
bmp = BitmapFactory.decodeFile(imagePath, options);
} catch (OutOfMemoryError exception) {
exception.printStackTrace();
}
try {
scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888);
} catch (OutOfMemoryError exception) {
exception.printStackTrace();
}
floatratioX= actualWidth / (float) options.outWidth;
floatratioY= actualHeight / (float) options.outHeight;
floatmiddleX= actualWidth / 2.0f;
floatmiddleY= actualHeight / 2.0f;
MatrixscaleMatrix=newMatrix();
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
Canvascanvas=newCanvas(scaledBitmap);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, newPaint(Paint.FILTER_BITMAP_FLAG));
ExifInterfaceexif=null;
try {
exif = newExifInterface(imagePath);
intorientation= exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
Matrixmatrix=newMatrix();
if (orientation == 6) {
matrix.postRotate(90);
} elseif (orientation == 3) {
matrix.postRotate(180);
} elseif (orientation == 8) {
matrix.postRotate(270);
}
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStreamout=newByteArrayOutputStream();
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 85, out);
byte[] byteArray = out.toByteArray();
BitmapupdatedBitmap= BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
return updatedBitmap;
}
privateintcalculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
finalintheight= options.outHeight;
finalintwidth= options.outWidth;
intinSampleSize=1;
if (height > reqHeight || width > reqWidth) {
finalintheightRatio= Math.round((float) height / (float) reqHeight);
finalintwidthRatio= Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
finalfloattotalPixels= width * height;
finalfloattotalReqPixelsCap= reqWidth * reqHeight * 2;
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++;
}
return inSampleSize;
}
}
after that set imagejust for example use your image view and set image
preview_bitmap = ImageUtils.getInstant().getCompressedBitmap(pathName);
Post a Comment for "Android Orientation Issue On Samsung Devies With Exif"