【问题标题】:camera intent rotate image before saving into sd card相机意图在保存到 SD 卡之前旋转图像
【发布时间】:2014-02-23 04:43:38
【问题描述】:

我搜索了很长时间以获得有关在保存到 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();
            }       
}

【问题讨论】:

标签: java android


【解决方案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);

【讨论】:

  • 我把这段代码放在我的 onActivityResult 中。结果还是一样。
  • 还是一样。我正在使用 Galaxy y 进行测试。代码更新
  • 即使我的方向 = 6 和 matrix.postRotate(90) 也不会旋转
  • @hasper 很高兴为您提供帮助:)
【解决方案2】:

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

看看下面的代码。希望这会有所帮助:

Camera Snippet

这个 sn-p 还包含自动对焦启用/禁用、快门声音等。享受吧!

【讨论】:

  • 感谢分享。你们之间有任何带有呼叫意图 ACTION_IMAGE_CAPTURE 的样本吗?
  • nope.. 但是你可以找到很多这样的例子/教程。这是 developer.android.com 上的一个:developer.android.com/training/camera/photobasics.htmldeveloper.android.com/guide/topics/media/…
  • 我的意思是示例调用意图 ACTION_IMAGE_CAPTURE 在保存到 SD 卡之前以正确的方式旋转。
  • 好的..没试过..所以不能说..如果我找到任何东西会回复你。也请让我更新。 :)
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多