0

我搜索了很长时间以获得有关在保存到 SD 卡之前使用相机意图旋转图像的解决方案。我尝试以纵向拍摄照片并进入 sd 卡文件路径以将其显示为横向。有人知道如何解决这个问题吗?我的代码如下: -

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

    @Override

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {


    Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC");
                if(cursor != null && cursor.moveToFirst())
                {
                    do {
                        uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));

                    photoPath = uri.toString();

Matrix matrix = new Matrix();

        ExifInterface exifReader = null;
        try {
            exifReader = new ExifInterface(photoPath);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }// Location of your image

        int orientation = exifReader.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        if (orientation ==ExifInterface.ORIENTATION_NORMAL) {

        // Do nothing. The original image is fine.
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {

               matrix.postRotate(90);

        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {

               matrix.postRotate(180);

        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {

               matrix.postRotate(270);

        }
                }while(cursor.moveToNext());
                cursor.close();
            }       
}
4

3 回答 3

1

使用下面的代码来旋转你的图像:

Uri imageUri = intent.getData();
            String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
            Cursor cur = managedQuery(imageUri, orientationColumn, null, null, null);
            int orientation = -1;
            if (cur != null && cur.moveToFirst()) {
                orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
            }  
            Matrix matrix = new Matrix();
            matrix.postRotate(orientation);
于 2013-10-25T05:56:08.807 回答
0

我最近遇到了类似的问题,我花了很长时间才弄清楚这一点。这可能不是一个很好的解决方案,但对我来说效果很好。

看看下面的代码。希望这可以帮助:

相机片段

该片段还包含自动对焦启用/禁用、快门声音等。享受吧!

于 2013-10-25T05:51:52.100 回答
-1

嗨,看看下面的代码。在保存捕获的图像之前,请执行以下过程。它将以纵向模式保存图像。希望这会帮助你。

int rotation = -1;
 rotation = ((WindowManager)getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay().getOrientation();



    Matrix rotator = new Matrix();
    switch (rotation) {
    case (Surface.ROTATION_0):
        break;
    case (Surface.ROTATION_90):
        rotator.postRotate(270);
        break;
    case (Surface.ROTATION_180):
        rotator.postRotate(180);
        break;
    case (Surface.ROTATION_270):
        rotator.postRotate(90);
        break;


    // screen_{width,height} are applied before the rotate, so we don't
    // need to change them based on rotation.
    bmp_ss = Bitmap.createBitmap(bmp_ss, 0, 0, screen_width, screen_height, rotator, false);
于 2013-10-25T04:54:10.687 回答