在三星手机中拍摄照片时如何旋转90度?

7

在三星手机上,从相机捕获照片时会旋转90度,而在其他手机(如HTC)上则正常工作。请帮我解决这个问题。

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, IMAGE_CAPTURE); 

@Override 
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {     
    super.onActivityResult(requestCode, resultCode, data);    
      try
    {
    if (requestCode == IMAGE_CAPTURE) {
       if (resultCode == RESULT_OK){

       Uri contentUri = data.getData();
       if(contentUri!=null)
       {
        String[] proj = { MediaStore.Images.Media.DATA };         
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);         
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);         
        cursor.moveToFirst();         
        imageUri = Uri.parse(cursor.getString(column_index));
       }

       tempBitmap = (Bitmap) data.getExtras().get("data"); 
       mainImageView.setImageBitmap(tempBitmap);
       isCaptureFromCamera = true;
    }
 }

你期望竖屏图像方向吗? - Alex Cohn
不,我希望图像的方向与我拍摄时相同。例如,如果我以纵向模式拍摄照片,则应该是纵向的,横向亦然。请帮帮我。 - Ajay S
1
在不同的安卓设备上,包括三星,竖屏摄像头存在许多不同的错误。如果可能的话,请使用横屏方向,并通过使用旋转的UI元素来模拟纵向模式,就像原生相机应用程序一样。 - Alex Cohn
@ Alex Cohn,这个问题有没有解决方案? - Ajay S
我也遇到了这个问题,请看一下https://dev59.com/emoy5IYBdhLWcg3wlvGX,这解决了我的问题。 - ekatz
三星设备很糟糕。在三星设备上进行测试非常可怕。它只能以高清分辨率捕捉视频和图像。 - Mohanish
4个回答

11

一些设备会根据设备方向旋转图像。

在这里,我编写了一个通用方法来获取方向并使图像按正确比例显示。

    public  Bitmap decodeFile(String path) {//you can provide file path here 
        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 = 0;
            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("ExifInteface .........", "rotation ="+orientation);

//          exif.setAttribute(ExifInterface.ORIENTATION_ROTATE_90, 90);

            Log.e("orientation", "" + orientation);
            Matrix m = new Matrix();

            if ((orientation == ExifInterface.ORIENTATION_ROTATE_180)) {
                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 == ExifInterface.ORIENTATION_ROTATE_90) {
                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 == ExifInterface.ORIENTATION_ROTATE_270) {
                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;
        }

    }

编辑:

此代码未经过优化,我只是展示了来自我的一个测试项目的逻辑代码。


它可以正常工作,但在我连续捕捉三星Galaxy Tab2(7英寸)的快照时出现“内存不足异常”。请问你能提供一些解决方案吗? - Hasmukh
@Hasmukh,你可以更改比特图的比例大小,在这段代码中我使其可靠。 - dharmendra
是的,如果我给定比例类型为8,那么它可以正常工作,但我需要将其显示为预览图像,并且还需要将该大图像作为byte64上传。 - Hasmukh
@Hasmukh,内存错误有时取决于设备堆大小,有时我们必须小心处理那些位图,使用后需要回收位图。 - dharmendra
我使用这段代码时,无论我如何拿着我的HTC手机,方向始终为0。有人遇到同样的问题吗? - PaperThick
@PaperThick 你的设备可能以正确的方向拍摄了照片,或者你的设备方向模式可能关闭了。 - dharmendra

2
另外,您可以添加到上述解决方案的内容是"samsung".contentEquals(Build.MANUFACTURER)。如果您知道您的问题只涉及三星设备,则可以合理确定需要旋转返回的图像(仅当"samsung".contentEquals(Build.MANUFACTURER) && getActivity().getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT // && width > height//时)//在这里您知道需要旋转。
那么您可能会“合理地”认为需要旋转。

三星手机的照片宽度始终大于高度。只有在竖屏模式下才会出现错误。 - Peter

0

如果这确实是一个错误,那么您可能需要手动将其旋转回横向。位图数据始终具有宽度和高度,请查看数字,如果宽度小于高度,则根据alistair3408的答案旋转图像。


0
public static Bitmap rotateBitmap(Bitmap b, float degrees) {
    Matrix m = new Matrix();
    if (degrees != 0) {
        // clockwise
        m.postRotate(degrees, (float) b.getWidth() / 2,
                (float) b.getHeight() / 2);
    }

    try {
        Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
                b.getHeight(), m, true);
        if (b != b2) {
            b.recycle();
            b = b2;
        }
    } catch (OutOfMemoryError ex) {
        // We have no memory to rotate. Return the original bitmap.
    }
    return b;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接