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

4 回答 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;
        }

    }

编辑:

这段代码没有优化,我只是展示了我的一个测试项目中的逻辑代码。

于 2013-02-02T05:25:59.750 回答
2

您可以添加到上述解决方案的另一件事是"samsung".contentEquals(Build.MANUFACTURER). 如果您知道您的问题仅与三星设备有关,您可以合理地确定您需要旋转返回的图像(仅)if ("samsung".contentEquals(Build.MANUFACTURER) && getActivity().getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT // && width > height//) // here you know you need to rotate

你可以“合理地”相信轮换是有必要的。

于 2014-04-30T17:45:31.533 回答
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;
}
于 2012-12-15T18:46:07.447 回答
0

如果它真的是一个错误,那么您可能必须手动将其旋转回横向。位图数据始终具有宽度和高度,只需查看数字,如果宽度小于高度,则按照 alistair3408 的回答旋转图像。

于 2012-12-15T18:46:30.740 回答