Android:三星设备自动旋转图像

问题描述 投票:1回答:2

我创建了一个自定义相机。当我单击应用程序中的捕获按钮时,已拍摄图像。此外,我在名为onPictureTaken的函数中以字节数组的形式获取数据。

我正在使用称为Glide的库将字节数组转换为位图。

我的问题是在三星设备中图像会自动旋转。我已经研究了很长一段时间了。我发现这个库被称为元数据提取库,用于从byte []获取Exif信息并在其上旋转图像,但它不能在Samsung设备上运行。每次对于肖像图像返回值为1的元数据提取库,其显示图像不需要旋转,但是以纵向模式拍摄的图像总是旋转90度。

每当照片以纵向模式拍摄时,前照相机和后照相机都会以90度的角度旋转,元提取库显示的值为1。

有没有其他东西然后元数据提取提取库提取Exif信息流数据?

注意:我不能使用ExifInterface,因为它要求最低Api水平为24,而我在API级别22上进行测试

我尝试了很多解决方案,但没有任何工作。这有什么解决方案吗?

代码如下:

public void onPictureTaken(byte[] data, Camera camera) {
            mCamera.stopPreview();

            Glide.with(this).load(data)
    .asBitmap().centerCrop().animate(R.anim.abc_fade_in)
    .into(new SimpleTarget<Bitmap>(width, height) {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                camera_view.setVisibility(View.INVISIBLE);
                                int w = resource.getWidth();
                                int h = resource.getHeight();

                                // Setting post rotate to 90

                                Matrix mtx = new Matrix();


                                try {

                                    InputStream is = new ByteArrayInputStream(data);
                                    Metadata metadata = ImageMetadataReader.readMetadata(is);
                                    final ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
                                    if (exifIFD0Directory.containsTag(ExifIFD0Directory.TAG_ORIENTATION)) {
                                        final int exifOrientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
                                        switch (exifOrientation) {

                                            case 6:
                                                mtx.postRotate(90);
                                                break;  // top left
                                            case 3:
                                                mtx.postRotate(180);;
                                                break;  // top right
                                            case 8:
                                                mtx.postRotate(270);
                                                break;  // bottom right

                                        }
                                        photo = Bitmap.createBitmap(resource, 0, 0, w, h, mtx, true);
                                            /* Work on exifOrientation */
                                    } 


                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }

        }

我正在使用三星J5进行测试。

android image android-glide metadata-extractor
2个回答
1
投票

你不需要一个库。这里有几个我写的方法应该为你做的伎俩。

 public static int getCapturedImageOrientation(Context context, Uri imageUri){
    int rotate = 0;
    try {
        context.getContentResolver().notifyChange(imageUri, null);
        File imageFile = new File(imageUri.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;
        }

        Log.i("RotateImage", "Exif orientation: " + orientation);
        Log.i("RotateImage", "Rotate value: " + rotate);
    } catch (Exception e) {
        Log.e(TAG, "Error getting rotation of image");

    }

    return rotate;
}
public static int GetRotateAngle(Context context, Uri imageUri) {
    String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION };
    Cursor cursor = context.getContentResolver().query(imageUri, columns, null, null, null);
    if (cursor == null) {
        //If null, it is not in the gallery, so may be temporary image
        return getCapturedImageOrientation(context, imageUri);

    }

    cursor.moveToFirst();

    int orientationColumnIndex = cursor.getColumnIndex(columns[1]);
    int orientation = cursor.getInt(orientationColumnIndex);
    cursor.close();
    return orientation;
}

我将它们包装在一个名为ImageHelper的类中。你可以像这样使用它:

 rotateImage(ImageHelper.GetRotateAngle(Context, mCropImageUri));

然后当然rotateImage代码将是:

 private void rotateImage(int degrees) {
        Matrix mat = new Matrix();
        mat.postRotate(degrees);
        mCropImage = Bitmap.createBitmap(mCropImage, 0, 0, mCropImage.getWidth(), mCropImage.getHeight(), mat, true);
        setImageForCropping(mCropImage);

    }

当然,我正在为照片编辑,裁剪和缩放应用程序做所有这些,所以你可以忽略一些额外的东西,但这应该照顾你。祝好运。


0
投票

几乎在所有三星设备中,图像轮换问题很常见,在我的情况下我使用三星Note 3并且出现同样的问题,但我使用下面的代码来解决这个问题

public static Bitmap decodeFile(String path) { // this method is for avoiding the image rotation
        int orientation;
        try {
            if (path == null) {
                return null;
            }
            // decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            // Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE = 70;
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 4;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale++;
            }
            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            Bitmap bm = BitmapFactory.decodeFile(path, o2);
            Bitmap bitmap = bm;
            ExifInterface exif = new ExifInterface(path);
            orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
            Log.e("orientation", "" + orientation);
            Matrix m = new Matrix();
            if ((orientation == 3)) {
                m.postRotate(180);
                m.postScale((float) bm.getWidth(), (float) bm.getHeight());
//               if(m.preRotate(90)){
                Log.e("in orientation", "" + orientation);
                bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);
                return bitmap;
            } else if (orientation == 6) {
                m.postRotate(90);
                Log.e("in orientation", "" + orientation);
                bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);
                return bitmap;
            } else if (orientation == 8) {
                m.postRotate(270);
                Log.e("in orientation", "" + orientation);
                bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);
                return bitmap;
            }
            return bitmap;
        } catch (Exception e) {
        }
        return null;
    }

这段代码对我有用,所以我希望这对你有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.