Camera API - 纵向图像保存为横向

时间:2014-01-01 14:12:00

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

我尝试从相机意图中保存并填充图像,并将图像填充到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();
                    }

                }
        }
    }

1 个答案:

答案 0 :(得分:0)

来自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();