Android Rotate Image - android

I want to rotate an ImageButtons depand on the orientation. But without a restart of the activity. The Image should rotate, but the View shouldn't. [Example: the default Camera App] Any idea? I think I should fix the orientation (android:screenOrientation="portrait").
If you rotate the phone, the activity won't get rebuild. But the icons on the bottom (or the side) rotate. How can i do this?
example: http://www.youtube.com/watch?v=hT3stvtv_1c at 00:40 - just the icons rotate, not the hole view

Matrix matrix = new Matrix();
Bitmap b;
//...
imageView.setImageBitmap(b);
//...
// screen rotation
matrix.postRotate(90);
b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
imageView.setImageBitmap(b);

You can get the rotation info from the file using ExifInterface
Matrix matrix = new Matrix();
int orientation = 1;
try {
ExifInterface exif = new ExifInterface(filePath);
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
} catch (IOException e) {
e.printStackTrace();
}
matrix.setRotate(0);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(ROTATION_DEGREE);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(ROTATION_DEGREE * 2);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-ROTATION_DEGREE);
break;
default:
break;
}

If u want rotate image dynamically on click of button define globally
int angle; int valueangle = 0;
and the use onclick of button
mrotate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView mainimage22 = (ImageView) findViewById(R.id.mainimage22);
Bitmap myImg = BitmapFactory.decodeResource(getResources(), R.drawable.image);
angle = valueangle + 90;
valueangle = angle;
System.out.println("valueangle"+valueangle);
if(valueangle == 360){
valueangle=0;
System.out.println("00"+valueangle);
}
System.out.println("angle"+angle);
main_img.setVisibility(View.INVISIBLE);
Matrix matrix = new Matrix();
matrix.postRotate(angle);
Bitmap rotated = Bitmap.createBitmap(myImg , 0, 0,
myImg .getWidth(), myImg .getHeight(), matrix, true);
mainimage22.setImageBitmap(rotated);
}
});

I used OrientationEventListener and implemented the method onOrientationChanged(int orientation). In my MainActivity. which is force to be portrait I create an instance of the call and start tracking the rotation:
public class RotationDetector extends OrientationEventListener
in my MainActivity:
mOrientationListener = new RotationDetector(this);
mOrientationListener.enable();
Dont forget to disable() it, when you dont use it anymore. And watch out for MaliEGL errors.

Related

Getting Orientation as 0 and still getting the rotated image in ImageView in android

I have tried most of the code on stackoverflow but none of them are working.
I am using moto x4 for uploading picture using camera. when I use back camera it gets rotated 90 degree left and when I use front camera it gets rotated 90 degree right. but in debug mode, in both case I found the orientation = 0;
else if (requestCode == CAMERA) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
String imagePath = saveImage(thumbnail);
File imageFile = new File(imagePath);
ExifInterface exif = null;
try {
exif = new ExifInterface(imageFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
bmap = GetandSetBitmap.rotateBitmap(thumbnail,orientation);
mImageView.setBackgroundResource(0);
mImageView.setImageBitmap(bmap);
}
This probably has to do with the fact that one camera is by default in landscape and the other in reverse landscape, so orientation = 0, as the orientation is detected to be the normal one in both cases. Unfortunately, I don't have a solution that wouldn't involve manually rotating the image to cover all cases. Personally I've used a switch to cater for the cases where my photo was saved with a 90 degrees rotation:
ExifInterface exifInterface = new ExifInterface(pictureFile.getPath());
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
Bitmap correctedBitmap;
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
correctedBitmap = bitmap;
capturedImageHolder.setImageBitmap(correctedBitmap);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
correctedBitmap = rotateImage(bitmap, 90);
capturedImageHolder.setImageBitmap(correctedBitmap);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
correctedBitmap = rotateImage(bitmap, 180);
capturedImageHolder.setImageBitmap(correctedBitmap);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
correctedBitmap = rotateImage(bitmap, 270);
capturedImageHolder.setImageBitmap(correctedBitmap);
break;
}
} catch (IOException e) {
}
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
}
In your case, you would need to detect if the photo is taken from the front-facing camera or the back and adjust the values accordingly.

Rotate a bitmap using render script android

When I use following code, it ends up with outofmemory exception. After doing researh Render script looks like a good candidate. Where can I find sample code for similar operation and how can integrate it to my project.
public Bitmap rotateBitmap(Bitmap image, int angle) {
if (image != null) {
Matrix matrix = new Matrix();
matrix.postRotate(angle, (image.getWidth()) / 2,
(image.getHeight()) / 2);
return Bitmap.createBitmap(image, 0, 0, image.getWidth(),
image.getHeight(), matrix, true);
}
return null;
}
Basically rotating bitmap is a task of rotating 2D array without using additional memory. And this is the correct implementation with RenderScript: Android: rotate image without loading it to memory .
But this is not necessary if all you want is just to display rotated Bitmap. You can simply extend ImageView and rotate the Canvas while drawing on it:
canvas.save();
canvas.rotate(angle, X + (imageW / 2), Y + (imageH / 2));
canvas.drawBitmap(imageBmp, X, Y, null);
canvas.restore();
What about ScriptIntrinsic, since it's just a built-in RenderScript kernels for common operations you cannot do nothing above the already implemented functions: ScriptIntrinsic3DLUT, ScriptIntrinsicBLAS, ScriptIntrinsicBlend, ScriptIntrinsicBlur, ScriptIntrinsicColorMatrix, ScriptIntrinsicConvolve3x3, ScriptIntrinsicConvolve5x5, ScriptIntrinsicHistogram, ScriptIntrinsicLUT, ScriptIntrinsicResize, ScriptIntrinsicYuvToRGB. They do not include functionality to rotate bitmap at the moment so you should create your own ScriptC script.
Try this code..
private Bitmap RotateImage(Bitmap _bitmap, int angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
_bitmap = Bitmap.createBitmap(_bitmap, 0, 0, _bitmap.getWidth(), _bitmap.getHeight(), matrix, true);
return _bitmap;
}
Use this code when select image from gallery.
like this..
File _file = new File(file_name);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap bitmap = BitmapFactory.decodeFile(file_name, options);
try {
ExifInterface exif = new ExifInterface(file_name);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
bitmap = RotateImage(bitmap, 90);
} else if (orientation ==ExifInterface.ORIENTATION_ROTATE_270) {
bitmap = RotateImage(bitmap, 270);
}
} catch (Exception e) {
e.printStackTrace();
}
image_holder.setImageBitmap(bitmap);

how to automatically turn the picture in the right form (library cropper )?

I use library cropper here in my project.
Everything works cool, but if a photo was made in portrait mode, it flips and is displayed in landscape mode.
Here is an original photo - original
This is opened by the program - program
There is a method in the library cropImageView.rotateImage(90); I can add a button that rererses the picture when pressed. But what can I do to make it run once loaded in the right mode?
imageCrop = (CropImageView) v.findViewById(R.id.imageCrop);
String picturePath = getArguments().getString("image");
crudeImage = BitmapFactory.decodeFile(picturePath);
imageCrop.setImageBitmap(crudeImage);
You need to decode the original image into bitmap in its orientation. Detect image orientation using ExifInterface, then use Matrix to rotate your bitmap to image's original orientation:
crudeImage = BitmapFactory.decodeFile(picturePath);
float rotationDegrees = getRotationDegreesToOriginal(picturePath);
if (rotationDegrees != 0f) {
crudeImage = rotateImage(crudeImage, rotationDegrees);
}
/**
* Rotate a bitmap by given degrees
* #param bitmap original bitmap
* #param degrees rotation degrees
* #return rotated bitmap
*/
public static Bitmap rotateImage(Bitmap bitmap, float degrees) {
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
/**
* Get rotation degrees that need to be applied to rotate an image to its original orientation
* #param fileName full path to image
* #return rotation degrees, or 0 if file not found
*/
public static float getRotationDegreesToOriginal(String fileName) {
final ExifInterface exif;
try {
exif = new ExifInterface(fileName);
} catch (IOException e) {
return 0f;
}
// Detect original orientation to rotate back to
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_180:
return 180f;
case ExifInterface.ORIENTATION_ROTATE_90:
return 90f;
case ExifInterface.ORIENTATION_ROTATE_270:
return 270f;
default:
return 0f;
}
}

ImageView setRotation having an aliased or jagged edges

Hi currently I'm trying to display an image that is rotated by about 5 degrees on an imageview after I capture it and rotate the image into proper orientation (camera issues). Now the problem is after setting the image on an imageview which is rotated 5 degrees sing imageview.setRotation(5) the imageview itself becomes aliased. I'm pretty sure that the imageview is the one being aliased since my image has a padding of 5dp to have a white border on the sides but please do correct me if I'm wrong.
What solution I tried so far is this but the sample solution works for already set image on a custom view and I'm not that used to in configuring or further customizing a custom views to work based on what I need. though his problem is almost the same as mine.
I also tried this one which apply antialiasing on the fly but didn't work as well.
So far here is my code:
imageview = (ImageView) findViewById(R.id.img_preview);
imageview.setRotation(5);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String img_path = extras.getString("img_path");
File image_file = new File(img_path);
try {
Bitmap captured_image = applyOrientation(BitmapFactory.decodeFile(img_path),resolveBitmapOrientation(image_file));
imageview.setImageBitmap(captured_image);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And this is the code on where I rotate my image maybe someone can help fix it from here too so here it is:
private int resolveBitmapOrientation(File bitmapFile) throws IOException {
ExifInterface exif = null;
exif = new ExifInterface(bitmapFile.getAbsolutePath());
return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
}
private Bitmap applyOrientation(Bitmap bitmap, int orientation) {
int rotate = 0;
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;
default:
return bitmap;
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}

Rotating image decentres image using janmuller library

I am using janmuller android library to perform rotation of an image . On rotating it 90 degrees to the left or right the image is rotated , however it gets de centred on the screen . This is the code I have used from janmuller library
public void onRotateRight(View v) {
mBitmap = Util.rotateImage(mBitmap, 90);
RotateBitmap rotateBitmap = new RotateBitmap(mBitmap);
mImageView.setImageRotateBitmapResetBase(rotateBitmap, true);
mRunFaceDetection.run();
}
This is the rotateImage function
public static Bitmap rotateImage(Bitmap src, float degree) {
// create new matrix
Matrix matrix = new Matrix();
// setup rotation degree
matrix.postRotate(degree);
Bitmap bmp = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
return bmp;
}
public void onRotateRight(View v) {
mBitmap = Util.rotateImage(mBitmap, 90);
RotateBitmap rotateBitmap = new RotateBitmap(mBitmap);
mImageView.setImageRotateBitmapResetBase(rotateBitmap, true);
mRunFaceDetection.run();
}
I wonder why you have so much code. Especially i wonder that after having used rotateImage() on the bitmap you dont put it back in the imageview. There is so much code with rotate (why is that needed?) and reset base (what does that?) that it is difficult to say who is to blame when things go wrong. Better try with a normal ImageView first before you blame janmuller. More like this:
public void onRotateRight(View v) {
mBitmap = Util.rotateImage(mBitmap, 90);
mImageView.setImageBitMap(mBitmap);
}
That should be enough to test;
The function rotateImage() looks ok.

Categories

Resources