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);
}
Related
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.
Issues facing on Samsung, when I am capturing the image, the captured image get rotated. I am trying to rotate the image in the vertical direction 90 degree. On Back Facing the image get rotated with below code. But two images get stored on the device. Also issues facing while I am capturing the image on front face the image get rotated, how I can handle the image rotation in below code for both the front and back facing. Any ideas ?
try {
upload_bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
large_bitmap = scaleDownLargeImageWithAspectRatio(upload_bitmap);
String fileNameLarge = "myapp_" + System.currentTimeMillis() + ".jpg";
large_bitmap_path = MediaStore.Images.Media.insertImage(getContentResolver(), large_bitmap, fileNameLarge, null);
//Start change orientation
String device_name = Build.MANUFACTURER;
if(device_name.equals("samsung"))
{
int rotate=0;
try {
String realPath = getRealPathFromUri(CameraActivity.this, Uri.parse(large_bitmap_path));
exif = new ExifInterface(realPath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
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:
case ExifInterface.ORIENTATION_UNDEFINED:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
Matrix matrix = new Matrix();
matrix.postRotate(90);
large_bitmap = Bitmap.createBitmap(large_bitmap, 0, 0, large_bitmap.getWidth(), large_bitmap.getHeight(), matrix, true);
horizontalList.add(large_bitmap_path);
horizontal_rv.smoothScrollBy(1000, 10);
sqLiteHelper.insertPath(large_bitmap_path, "jpg", "fileName");
}
use this library from git hub it handles all these thing related to image library link
I creating a custom camera application in which after taking the photo I am setting the bitmap in the imageview. I am using the library known as GLIDE. To rotate image in the right direction I am getting the Exif information from Input stream.
Once I take the portrait photo the onPictureTaken returns the byte[]. After checking the byte[] the Exif tag returned is ExifInterface.ORIENTATION_NORMAL which means the rotation of image is normal and it does not need any more rotation. However, the image set by the Glide in the image view is in landscape instead of portrait and it requires rotation of 90 degree .May be there is something in the glide library that rotates the image automatically. Does someone has any idea?
Any help is appreciated. The code is given below:
public void onPictureTaken(byte[] data, Camera camera) {
Glide.with(this)
.load(data)
.asBitmap().override(height, width) // resizes the image to these dimensions (in pixel)
.centerCrop()
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
try {
InputStream is = new ByteArrayInputStream(data);
Metadata metadata = ImageMetadataReader.readMetadata(is);
final ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
Matrix mtx = new Matrix();
int w = bitmap.getWidth();
int h = bitmap.getHeight();
if (exifIFD0Directory.containsTag(ExifIFD0Directory.TAG_ORIENTATION)) {
final int exifOrientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION); //checking Exif here it returns image is normal
switch (exifOrientation) {
case ExifInterface.ORIENTATION_NORMAL:
break;
case ExifInterface.ORIENTATION_ROTATE_90:
mtx.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
mtx.postRotate(180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
mtx.postRotate(270);
break;
}
}
photo = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
Imageview.setImageBitmap(photo);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
I am making an android app that sends pictures the user explicitly took and send it to a web server. Next, i display those pictures in a web application.
However, the pictures taken from the smartphone in portrait appear in the server rotated as if they were taken in landscape mode and vice versa.
Any idea why this is happening?
There is a property of the image, "exif tag". Which tells about the orientation of the image. You can check the value of this tag before sending the image to server.
you can use below method to get the unrotated image
public final static Bitmap getUnRotatedImage(String imahePath, Bitmap rotattedBitmap)
{
int rotate = 0;
try
{
File imageFile = new File(imahePath);
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)
{
e.printStackTrace();
return null;
}
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
return Bitmap.createBitmap(rotattedBitmap, 0, 0, rotattedBitmap.getWidth(), rotattedBitmap.getHeight(), matrix,
true);
}
It takes two arguments, image path and the bitmap image.
Try this method before sending the images to the server.
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.