【问题标题】:Photo rotate 90 degree while capture in some phones在某些手机中拍摄时照片旋转 90 度
【发布时间】:2014-04-26 20:29:58
【问题描述】:

照片旋转 90 度,同时从三星手机中的相机捕获其他手机的其他手机工作正常。请帮帮我。

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

【问题讨论】:

    标签: android android-camera


    【解决方案1】:

    这恰好是早期 Android 版本中的错误。

    我解决了这个问题,只是获取了方向角度并相应地旋转了位图。

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

    【讨论】:

    • 如果有人会使用您的解决方案,我只想提一下,我认为您以错误的方式计算比例。正如您在代码注释中所说,如果比例是 2 的幂会更好。像您一样将规模增加一并不会向我们保证它将是二的幂。如果您想尽可能接近所需的正确宽度和高度,则应在每次迭代中将比例乘以 2。
    【解决方案2】:

    还可以查询 MediaStore.Images.Media.ORIENTATION 值以获取旋转角度。然后你可以自己旋转图像或其他任何东西。

    【讨论】:

    • 它在某些手机上运行良好,但在某些供应商手机上无法正常工作,我如何识别这个应该在哪个位置旋转图像
    • 如果供应商没有正确实现他们的代码,我想你无能为力......
    【解决方案3】:

    我在做什么: 首先使用相机的元数据信息检查相机拍摄的图像的方向,如果我们在纵向中找到它,则必须将图像旋转 90 度并显示,否则仅显示。

    为了获取有关图像方向的信息,我们可以使用 ExifInterface。 就是这样!

    【讨论】:

    • 您在此处重复的相同答案没有更多意义,即使在我的答案中有更好的解释
    猜你喜欢
    • 2012-11-06
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 2013-03-22
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多