本文介绍了Android开摄像头的位图的方向?而转回-90度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个code:

//choosed a picture
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {
        if (requestCode == ImageHelper.SELECT_PICTURE) {

            String picture           = "";

            Uri selectedImageUri     = data.getData();
            //OI FILE Manager
            String filemanagerstring = selectedImageUri.getPath();
            //MEDIA GALLERY
            String selectedImagePath = ImageHelper.getPath(mycontext, selectedImageUri);

            picture=(selectedImagePath!=null)?selectedImagePath:filemanagerstring;

...

这是唯一一张照片选择器,从画廊。这是很好的,但是当我打开这张照片上的ImageView,图像上的肖像模式用相机拍摄的时候好看,但拍了风景模式的相机,开在-90度的图像。

this is only a picture chooser, from gallery. this is nice, but when i opening this picture on an imageview, the images when took on "PORTRAIT MODE" with the camera look nice, but the images that took "LANDSCAPE MODE" with the camera, opening in -90 degrees.

我如何可以旋转这些照片回来?

How can i rotate those pictures back?

    Bitmap output       = Bitmap.createBitmap(newwidth, newheight, Config.ARGB_8888);
    Canvas canvas       = new Canvas(output);

我想这样的:

i tried this:

Log.e("w h", bitmap.getWidth()+" "+bitmap.getHeight());
if (bitmap.getWidth()<bitmap.getHeight()) canvas.rotate(-90);

但这是不工作,所有的图像尺寸为:2560 * 1920像素(纵向和横向模式下,所有)

but this is not working, all image size is: *2560 1920 pixel (PORTRAIT, and LANDSCAPE mode all)

我能做些什么转回的风景图片?

What can I do to rotate back the LANDSCAPE pictures?

感谢张国荣

推荐答案

如果照片是用数码相机或智能手机,旋转通常存储在照片的的的数据,作为图像文件的一部分。您可以读取图像的Exif采用的是Android <$c$c>ExifInterface.

If a photo is taken with a digital camera or smartphone, rotation is often stored in the photo's Exif data, as part of the image file. You can read an image's Exif meta-data using the Android ExifInterface.

首先,创建 ExifInterface

ExifInterface exif = new ExifInterface(uri.getPath());

接下来,找到当前旋转:

Next, find the current rotation:

int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

EXIF​​旋转转换为度:

Convert exif rotation to degrees:

int rotationInDegrees = exifToDegrees(rotation);

其中

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;
 }

然后利用图像的实际转速作为参考点使用 矩阵

Matrix matrix = new Matrix();
if (rotation != 0f) {matrix.preRotate(rotationInDegrees);}

您创建一个 Bitmap.createBitmap 方法,它接受一个矩阵的新旋转的图像作为一个参数:

You create the new rotated image with the Bitmap.createBitmap method that take a Matrix as a parameter:

Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

其中,矩阵M 保存新的旋转:

Bitmap adjustedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, width, height, matrix, true);

请参阅这些教程为有用的信息来源$ C ​​$ C的例子:

See these tutorials for useful source code examples:

这篇关于Android开摄像头的位图的方向?而转回-90度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:02