简体   繁体   English

Camera API-人像图像另存为风景

[英]Camera API - Portrait image is saved as landscape

I have tried to save and populate an image from the camera intent and populate the image into the ImageView. 我试图保存并从相机意图填充图像,然后将图像填充到ImageView中。 I tried to take a landscape picture and it worked well. 我试图拍一张风景照,效果很好。 The image is populated in the ImageView in landscape. 图像以横向方式填充在ImageView中。 However the problem arise when I tried to take a picture in portrait in the camera intent. 但是,当我尝试以相机意图拍摄肖像照片时会出现问题。 The image preview before saving was still in portrait but when the results was returned to my ImageView, the image is in landscape. 保存之前的图像预览仍为纵向,但是当结果返回到我的ImageView时,图像为横向。

Below is the code i used. 下面是我使用的代码。

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

                }
        }
    }

From the docs of Camera.Parameters#setRotation() 来自Camera.Parameters#setRotation()的文档

If applications want to rotate the picture to match the orientation of what users see, apps should use OrientationEventListener and Camera.CameraInfo 如果应用程序要旋转图片以匹配用户看到的方向,则应用程序应使用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 is an abstract class that contains the onOrientationChanged(). OrientationEventListener是一个抽象类,其中包含onOrientationChanged()。 This method will receive orientation changes from the system. 此方法将从系统接收方向更改。 This is one possible implementation: 这是一种可能的实现:

OrientationEventListener listener = new OrientationEventListener(this,
        SensorManager.SENSOR_DELAY_NORMAL) {

    @Override
    public void onOrientationChanged(int i) {
       //Code from above goes here.
    }
};
listener.enable();

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

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