Capturing image from camera, results in blowing up the orientation - android

I am trying to capture image from the camera, it works fine with landscape mode, when I take the picture in portrait it is rotated. Below is the code I am using:
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(largeImagePath, bounds);
Bitmap bm = BitmapFactory.decodeFile(largeImagePath, opts);
ExifInterface exif = new ExifInterface(largeImagePath);
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
System.out.println("Orientation camera:"+orientString);
int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90){
rotationAngle = 90;
System.out.println("In 90 degrees rotate");
}
if (orientation == ExifInterface.ORIENTATION_ROTATE_180){
rotationAngle = 180;
System.out.println("In 180 degrees rotate");
}
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
rotationAngle = 270;
System.out.println("In 270 degrees rotate");
}
Matrix matrix = new Matrix();
System.out.println("Rotation Angle"+rotationAngle);
matrix.setRotate(rotationAngle, (float) thumbnail.getWidth() / 2, (float) thumbnail.getHeight() / 2);
bitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
On Debug, the control enters the :
if (orientation == ExifInterface.ORIENTATION_ROTATE_90){
rotationAngle = 90;}
block, however no rotation to set the picture back to correct happens.
This is my XML:
<ImageView
android:id="#+id/imageView1"
android:layout_width="140dp"
android:layout_height="160dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:src="#drawable/btn_upload" />
UPDATE: I am getting the following exception:
02-27 13:51:55.126: I/System.out(16549): Exceptionjava.lang.IllegalArgumentException: x + width must be <= bitmap.width()

you can arrange your code as follow...
Bitmap bm = BitmapFactory.decodeFile(largeImagePath, opts);
ExifInterface ei;
try {
ei = new ExifInterface(largeImagePath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
bitmap = rotateImage(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
bitmap = rotateImage(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
bitmap = rotateImage(bitmap, 270);
break;
}
} catch (IOException e) {
e.printStackTrace();
}
write rotateImage() method as follows....
private Bitmap rotateImage(Bitmap source, float angle) {
Bitmap bitmap = null;
Matrix matrix = new Matrix();
matrix.postRotate(angle);
try {
bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
} catch (OutOfMemoryError err) {
err.printStackTrace();
}
return bitmap;
}

Use bm.getWidth() and bm.getHeight() instead bounds.outWidth and bounds.outHeight in createBitmap() method.

Related

Why Picture Rotate after taking Pic from android:hardware:camera api

I am using android:hardware:camera API to make custom camera . i have done to make the camera but problem is that after taking picture from camera the pic save in rotated form.I am using Surface View for the Camera .
this is Camera setting function on surface-created
try {
File f = new File(imagePath);
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
angle = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
angle = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
angle = 270;
}
Matrix mat = new Matrix();
mat.postRotate(angle);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f),
null, options);
bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), mat, true);
ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100,
outstudentstreamOutputStream);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
Log.w("TAG", "-- Error in setting image");
} catch (OutOfMemoryError oom) {
Log.w("TAG", "-- OOM Error in setting image");
}
try this after capture image from camera to rotate.
You can use below method to show/save image in actual orientation.
public static Bitmap setImage(String filePath, Bitmap bitmap){
File curFile = new File(filePath);
Bitmap rotatedBitmap = null;
try {
ExifInterface exif = new ExifInterface(curFile.getPath());
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = exifToDegrees(rotation);
Matrix matrix = new Matrix();
if (rotation != 0.0f) {matrix.preRotate(rotationInDegrees);}
rotatedBitmap = Bitmap.createBitmap(bitmap,0,0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}catch(IOException ex){
ex.printStackTrace();
}
return rotatedBitmap;
}
private static int exifToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; }
return 0;
}

Why Images are rotated 90 degree in ImageView?

Why images are rotated 90 degree in imageView?? and how to fix it??
All images in gallery are not rotated 90 degree in imageView.
I don't know why some images are rotated 90 degree in imageView.
#Override
protected void onActivityResult(int requestCode , int resultCode , Intent data){
if(requestCode == 100){
if(resultCode == Activity.RESULT_OK){
try{
ImageView imageView = (ImageView)View.inflate(this, R.layout.imagelayout, null);
Uri imgUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imgUri);
int height = bitmap.getHeight();
int width = bitmap.getWidth();
int newHeight = height;
int newWidth = width;
float rate = 0.0f;
if(width > height ){
if(imageFlipper.getWidth() < width ){
rate = imageFlipper.getWidth() / (float) width ;
newHeight = (int) (height * rate);
newWidth = imageFlipper.getWidth();
}
}else{
if(imageFlipper.getHeight() < height ){
rate = imageFlipper.getHeight() / (float) height ;
newWidth = (int) (width * rate);
newHeight = imageFlipper.getHeight();
}
}
Bitmap reSize = Bitmap.createScaledBitmap(bitmap , newWidth , newHeight,true);
imageView.setImageBitmap(reSize);
//imageView.setImageURI(imgUri);
imageFlipper.addView(imageView);
imageFlipper.setDisplayedChild(imageFlipper.getChildCount() - 1);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
In some devices, when the camera is launched the orientation would change. In one of my apps, I also faced this issue. To handle this we need to find the orientation and rotate the picture accordingly.
// capture image orientation
public int getCameraPhotoOrientation(Context context, Uri imageUri,
String imagePath) {
int rotate = 0;
try {
context.getContentResolver().notifyChange(imageUri, null);
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
Log.i("RotateImage", "Exif orientation: " + orientation);
Log.i("RotateImage", "Rotate value: " + rotate);
} catch (Exception e) {
e.printStackTrace();
}
return rotate;
}
We need to use this integer returned to set the angle for Imageview.
int rotateImage = getCameraPhotoOrientation(this, uriLargeImage,
mediaFile.getAbsolutePath());
articleview.setRotation(rotateImage); // articleview is ImageView
So basically, please find the orientation of the photo as and when it is taken. This is in ExifInterface. Use this information to rotate.
Hope this helps. All the best.
You must set the rotation in the imageview with which the photo was taken:
imageView.setRotation(getCameraPhotoOrientation(filepath));
imageView.setImageURI( Uri.fromFile(filepath));
public static int getCameraPhotoOrientation(String imagePath) {
int rotate = 0;
try {
ExifInterface exif = null;
try {
exif = new ExifInterface(imagePath);
} catch (IOException e1) {
e1.printStackTrace();
}
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, 0);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 90;
break;
default:
rotate = 0;
}
} catch (Exception e) {
e.printStackTrace();
}
return rotate;
}
As mentioned before it does it automatically. The way I fixed this, was by using this ImageCropper, which counter-rotates it automatically, which is amazing.
Click here: https://github.com/ArthurHub/Android-Image-Cropper
So not only do you get to crop and rotate your image, but it fixes the problem too!!
/**
* Rotate a bitmap clockwise and anticlockwise
*/
btn_clock = (Button) findViewById(R.id.btn_clockWise);
btn_clock.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Matrix mMatrix = new Matrix();
Matrix mat=img.getImageMatrix();
mMatrix.set(mat);
mMatrix.setRotate(90);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), mMatrix, false);
img.setImageBitmap(bitmap);
}
});
btn_antiClock = (Button) findViewById(R.id.btn_AnticlockWise);
btn_antiClock.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Matrix mMatrix = new Matrix();
Matrix mat=img.getImageMatrix();
mMatrix.set(mat);
mMatrix.setRotate(-90);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), mMatrix, false);
img.setImageBitmap(bitmap);
}
});
Kotlin answer
use:
implementation 'androidx.exifinterface:exifinterface:VERSION'
Get newest release here: https://developer.android.com/jetpack/androidx/releases/exifinterface
Function:
private fun adjustRotation(file: File): Bitmap{
lateinit var rotatedBitmap: Bitmap
val bitmap = BitmapFactory.decodeFile(file.absolutePath)
val exifInterface = ExifInterface(file)
val orientation: Int = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED)
fun rotateBitmap(source: Bitmap, angle: Int): Bitmap{
val matrix = Matrix()
matrix.postRotate(angle.toFloat())
return Bitmap.createBitmap(source, 0, 0, source.width, source.height, matrix, true)
}
when (orientation){
ExifInterface.ORIENTATION_ROTATE_90 ->
rotatedBitmap = rotateBitmap(bitmap, 90)
ExifInterface.ORIENTATION_ROTATE_180 ->
rotatedBitmap = rotateBitmap(bitmap, 180)
ExifInterface.ORIENTATION_ROTATE_270 ->
rotatedBitmap = rotateBitmap(bitmap, 270)
ExifInterface.ORIENTATION_NORMAL ->
rotatedBitmap = bitmap
else -> rotatedBitmap = bitmap
}
return rotatedBitmap
}

how to set captured image using android in sumsung device is portrait mode

I'm trying below code to set captured image using android in sumsung device is portrait mode, but its set to landscape mode. Below code works fine for other devices excepts for samsung and sony.
Matrix mat = new Matrix();
ExifInterface exif = new ExifInterface(yourimagepath);
String orientstring = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientstring != null ? Integer.parseInt(orientstring) : ExifInterface.ORIENTATION_NORMAL;
int rotateangle = 0;
if(orientation == ExifInterface.ORIENTATION_ROTATE_90)
rotateangle = 90;
if(orientation == ExifInterface.ORIENTATION_ROTATE_180)
rotateangle = 180;
if(orientation == ExifInterface.ORIENTATION_ROTATE_270)
rotateangle = 270;
mat.setRotate(rotateangle, (float) bmpPic.getWidth() / 2, (float) bmpPic.getHeight() / 2);
File f = new File(yourimagepath);
Bitmap bmpPic = BitmapFactory.decodeStream(new FileInputStream(f), null, null);
Bitmap bmpPic1 = Bitmap.createBitmap(bmpPic, 0, 0, bmpPic.getWidth(), bmpPic.getHeight(), mat, true);
Is there is any other solution for same? Please suggest.
use this in your manifest file in activity tag..
android:screenOrientation="portrait"
It can set portrait mode for all devices.
Use ExifInterface to check the orientation of the image as stored in the device.
int rotate = 0;
try {
File imageFile = new File(uploadFile.getPath());
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
Then using matrix rotate the bitmap to the actual portrait or landscape as stored in device.
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inDither = true;
Bitmap bmp = BitmapFactory.decodeFile(uploadFile.getPath(), options);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmp, bmp.getWidth(), bmp.getHeight(), true);
Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
rotatedBitmap is the bitmap with correct orientation.
Hope it helps.
Try this code. I was used it on different phones include samsung and it works perfectly:
public Bitmap rotateImage(Bitmap bitmap) throws IOException {
//Rotate the image to get in correct position
ExifInterface exif = new ExifInterface(mImagePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
} else if (orientation == 8) {
matrix.postRotate(270);
} else if (orientation == 3) {
matrix.postRotate(180);
}
Bitmap rotatedImage = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return rotatedImage;
}
Hope it helps!

how to solve image capturing automatically rotating 90 degrees in android?

I captured image from camera and display image into imageview. It will display perfect but the captured image rotated 90 degrees automatically in some devices. I searched a lot about it but could not get proper solution. Please tell me the solution for it. Thanks in advance.
try this code
public Bitmap adjustImageOrientation(Bitmap image, File f) {
int rotate = 0;
try {
ExifInterface exif = new ExifInterface(f.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_NORMAL:
rotate = 0;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
if (rotate != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
image = Bitmap.createBitmap(image, 0, 0, image.getWidth(),
image.getHeight(), matrix, true);
}
return image;
}
Use this method to determine image rotation.
public int getCameraPhotoOrientation(Uri imageUri, String imagePath) {
int rotate = 0;
try {
_context.getContentResolver().notifyChange(imageUri, null);
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (Exception e) {
}
return rotate;
}
And if the method wouldn't return Zero rotated the image.
int degree=getCameraPhotoOrientation(Uri.fromFile(tempFile), fPath);
if (degree!= 0) {
bitmap = tools.rotateOrientationCall(bitmap, degree);
}
Rotate your bitmap.
public Bitmap rotateOrientationCall(Bitmap src, float degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
}

Image oriention issue capture from camera

I was facing a problem image capture from camera it was showing image in landscape even though i capture that image in portrait then i found code online. But i think there is some problem with this code it sometimes didn't work (show image in landscape). So what's wrong there
private void adjustImageOrientation()
{
ExifInterface exif;
int rotate = 0;
try
{
exif = new ExifInterface(filepath);
int exifOrientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (exifOrientation)
{
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
}
if (rotate != 0)
{
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// Setting pre rotate
Matrix mtx = new Matrix();
mtx.preRotate(rotate);
// Rotating Bitmap & convert to ARGB_8888, required by tess
try
{
bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
}
catch(OutOfMemoryError e)
{
e.printStackTrace();
//image = Bitmap.createBitmap(image, 0, 0, w/2, h/2, mtx, false);
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
// return image.copy(Bitmap.Config.ARGB_8888, true);
}

Categories

Resources