为什么相机拍摄时图像会旋转?

时间:2012-11-08 11:29:28

标签: android

在我的应用程序中,我通过摄像头捕获图像并将其路径保存在字符串变量(用于在服务器上发送图像的SD卡路径)上,同时我也在ImageView上设置该图像。但是图像会自动在横向上旋转,而不是以直角设置。我一直在StackOverflow和谷歌搜索并找到EXIF轮换使用这个:

http://mobisocial.stanford.edu/news/2011/08/rotating-images-in-android/

但它不起作用。我的代码是:         (在裁剪操作代码为onActivityResult之后)

case AppConstants.CROP_FROM_CAMERA:
        if (data != null) {
            Bundle extras = data.getExtras();

            if (extras != null) {
                Bitmap photo = extras.getParcelable("data");
                File file = new File("/sdcard/bidnear/");
                if (!file.isDirectory())
                    file.mkdir();
                imageUrl = "/sdcard/bidnear/thumbimgcrop.png";
                file = new File("/sdcard/bidnear/thumbimgcrop.png");
                try {
                    photo = rotateImage(photo,mImageCaptureUri);
                    photo.compress(Bitmap.CompressFormat.PNG, 100,
                            new FileOutputStream(file));
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                objimg.setBackgroundResource(0);
                objimg.setImageBitmap(photo);

            }
        }

旋转方法是:

private Bitmap rotateImage(Bitmap objbitmap,Uri uri)
{
    Matrix matrix = new Matrix();
    float rotation =rotationForImage(MyProfile.this, uri);
    if (rotation != 0f) {
         matrix.preRotate(rotation);
    }

    Bitmap resizedBitmap = Bitmap.createBitmap(
            objbitmap, 0, 0,80,80, matrix, true);
    return resizedBitmap;
}

它不起作用;我打开的相机和捕获代码是:

 private void setUserImage() {

    final String[] objimagechooseoptions = new String[] {
            AppConstants.SELECT_CAMERA, AppConstants.SELECT_GALLERY };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.select_dialog_item, objimagechooseoptions);
    AlertDialog.Builder objbuilder = new AlertDialog.Builder(this);
    objbuilder.setTitle("Select Image");

    objbuilder.setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) { // pick from
                                                                // camera
            if (item == 0) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                mImageCaptureUri = Uri.fromFile(new File(Environment
                        .getExternalStorageDirectory(), "tmp_avatar_"
                        + String.valueOf(System.currentTimeMillis())
                        + ".png"));
                intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                        mImageCaptureUri);
                try {
                    intent.putExtra("return-data", true);
                    startActivityForResult(intent,
                            AppConstants.PICK_FROM_CAMERA);
                } catch (ActivityNotFoundException e) {
                    e.printStackTrace();
                }
            } else { // pick from file
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,
                        "Complete action using"),
                        AppConstants.PICK_FROM_FILE);
            }
        }
    });
    final AlertDialog dialog = objbuilder.create();
    dialog.show();
}

1 个答案:

答案 0 :(得分:0)

点击this或尝试使用此

case AppConstants.CROP_FROM_CAMERA:
    if (data != null) {
        Bundle extras = data.getExtras();

        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");
            File file = new File("/sdcard/bidnear/");
            if (!file.isDirectory())
                file.mkdir();
            imageUrl = "/sdcard/bidnear/thumbimgcrop.png";
            file = new File("/sdcard/bidnear/thumbimgcrop.png");


    try {

    ExifInterface exif = new ExifInterface(filePath);
    orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
    //Toast.makeText(getApplicationContext(), ""+orientation, 1).show();
    Log.v("log", "ort is "+orientation);

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


    if (orientation==6)
        {
                    Matrix matrix = new Matrix();
                    matrix.postRotate(90);
                    photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
                }
                else if (orientation==8)
                {
                    Matrix matrix = new Matrix();
                    matrix.postRotate(270);
                    photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
                }

                else if (orientation==3)
                {
                    Matrix matrix = new Matrix();
                    matrix.postRotate(180);
                    photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
                }
                Log.v("log", "before width is "+photo.getWidth() + " and height is "+photo.getHeight());


            objimg.setBackgroundResource(0);
            objimg.setImageBitmap(photo);

        }
    }