简体   繁体   English

相机意图在保存到SD卡之前先旋转图像

[英]camera intent rotate image before saving into sd card

I search for quite long time to get the solution about using camera intent to rotate the image before saving into sd card. 我搜索了很长时间,以获取有关在保存到SD卡之前使用相机意图旋转图像的解决方案。 i try to capture a photo in portrait and go into sd card file path to view it show as landscape. 我尝试捕获人像照片并进入sd卡文件路径,以横向查看它。 Any got a idea how to solve this? 有人知道如何解决这个问题吗? My Code so far as below :- 我的代码到目前为止:

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

I recently faced a similar problem and it took me quite some time to figure this out. 我最近遇到了类似的问题,花了我很多时间才能解决。 This might not be a very good solution but it worked for me quite well. 这可能不是一个很好的解决方案,但对我来说效果很好。

Have a look at the following code. 看下面的代码。 Hope this helps: 希望这可以帮助:

Camera Snippet 相机片段

This snippet also contains autofocus enabling/disabling, shutter sound etc. Enjoy!! 此代码段还包含自动对焦启用/禁用,快门音等。欣赏!!

Use the code below to rotate your image: 使用下面的代码旋转图像:

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

Hi have a look at this below code. 嗨,看看下面的代码。 before saving your captured image do the following process. 在保存捕获的图像之前,请执行以下过程。 it will save the images in portrait mode. 它将以纵向模式保存图像。 hope this will help you. 希望这会帮助你。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM