【问题标题】:Image in gallery is saved with wrong orientation画廊中的图像以错误的方向保存
【发布时间】:2017-06-29 10:29:46
【问题描述】:


要拍照,我正在使用设备上安装的相机。 照片保存在图库中,但始终横向保存
我该如何解决?

这是点击“添加照片”按钮时的意图:

 private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                ex.printStackTrace();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = null;
                photoURI = galleryAddPic();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }

这是createImageFile()的方法:

private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        imageFileName = "JPG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

最后还是这个方法galleryAddPic()

private Uri galleryAddPic(){
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(mCurrentPhotoPath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
        return contentUri;
    }
        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }

将照片插入图库后,我可以随意旋转图像。 但是在图库中,图像没有很好地旋转。
所以我想以正确的方向将图像保存在画廊中。
谢谢!

【问题讨论】:

    标签: android android-camera android-camera-intent


    【解决方案1】:

    您必须从(EXIF 元数据)收集有关图片方向的信息,因为图片方向取决于设备内置的相机。 此代码从 EXIF 数据中读取信息并相应地旋转图像

    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file, bounds);
    
    BitmapFactory.Options opts = new BitmapFactory.Options();
    Bitmap bm = BitmapFactory.decodeFile(file, opts);
    ExifInterface exif = new ExifInterface(file);
    String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
    int orientation = orientString != null ? Integer.parseInt(orientString) :  ExifInterface.ORIENTATION_NORMAL;
    
    int rotationAngle = 0;
    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
    if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
    if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
    
    Matrix matrix = new Matrix();
    matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
    Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
    

    来自EXIF的信息:

    ExifInterface ei = new ExifInterface(photoPath);
    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                                         ExifInterface.ORIENTATION_UNDEFINED);
    
    switch(orientation) {
    
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotateImage(bitmap, 90);
            break;
    
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotateImage(bitmap, 180);
            break;
    
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotateImage(bitmap, 270);
            break;
    
        case ExifInterface.ORIENTATION_NORMAL:
    
        default:
            break;
    }
    

    旋转位图方法

    public static Bitmap rotateImage(Bitmap source, float angle) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
                                   matrix, true);
    }
    

    SAVE Bitmap 到画廊

    希望有帮助!!

    【讨论】:

    • 我已经在 imageview.and 函数中显示图像的方法中这样做了,但是我需要旋转位图,然后将其存储在图库中
    • 是的,但是在方法 addGalleryPic 之前,如果我调用 decodeBitmap(path),Bitmap 会很糟糕。拍照时,如何检索位图?
    • 你不是已经从这里得到 Bitmap 了吗? protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) { Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(照片); } }
    猜你喜欢
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多