【问题标题】:Camera API - Portrait image is saved as landscape相机 API - 纵向图像保存为横向
【发布时间】:2014-01-01 14:12:00
【问题描述】:

我尝试保存并填充来自相机意图的图像,并将图像填充到 ImageView 中。我试着拍一张风景照片,效果很好。图像以横向填充在 ImageView 中。但是,当我尝试在相机意图中拍摄肖像照片时,问题就出现了。保存前的图像预览仍然是纵向的,但是当结果返回到我的 ImageView 时,图像是横向的。

下面是我使用的代码。

private ContentValues values;
    private Uri imageUri;
    private final int PICTURE_RESULT = 1;
    private Bitmap thumbnail;
    private String imageurl;
btn_takeImage.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
//              Intent camera_intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//              startActivityForResult(camera_intent, CAMERA_PIC_REQUEST);

                values = new ContentValues();
                values.put(MediaStore.Images.Media.TITLE, "New Picture");
                values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
                imageUri = getContentResolver().insert(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, PICTURE_RESULT);
            }
        });
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {

        case PICTURE_RESULT:
            if (requestCode == PICTURE_RESULT)
                if (resultCode == Activity.RESULT_OK) {
                    try {
                        thumbnail = MediaStore.Images.Media.getBitmap(
                                getContentResolver(), imageUri);
                        img_backgroundImage.setImageBitmap(thumbnail);
                        imageurl = getRealPathFromURI(imageUri);    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
        }
    }

【问题讨论】:

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


    【解决方案1】:

    来自 Camera.Parameters#setRotation() 的文档

    如果应用想要旋转图片以匹配用户看到的方向,应用应该使用 OrientationEventListener 和 Camera.CameraInfo

    public void onOrientationChanged(int orientation) {
        if (orientation == ORIENTATION_UNKNOWN) return;
        android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(cameraId, info);
        orientation = (orientation + 45) / 90 * 90;
        int rotation = 0;
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
            rotation = (info.orientation - orientation + 360) % 360;
        } else {  // back-facing camera
            rotation = (info.orientation + orientation) % 360;
        }
        mParameters.setRotation(rotation);
    }
    

    OrientationEventListener 是一个包含 onOrientationChanged() 的抽象类。此方法将从系统接收方向更改。这是一种可能的实现方式:

    OrientationEventListener listener = new OrientationEventListener(this,
            SensorManager.SENSOR_DELAY_NORMAL) {
    
        @Override
        public void onOrientationChanged(int i) {
           //Code from above goes here.
        }
    };
    listener.enable();
    

    【讨论】:

    • 我是在相机意图之前还是在填充图像视图之前设置它?
    • 什么是cameraId和mParameters?
    猜你喜欢
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    相关资源
    最近更新 更多