在不使用位图的情况下旋转和保存捕获的图像。

时间:2022-12-24 08:57:36

I want to rotate and save my Captured Image using Android Native Camera to sdcard. As native cameras is in landscape by default. But the I don't want to use bitmap. Is it possible to do so ? please help. I am new to android development.

我想用安卓自带的相机到sdcard来旋转和保存捕获的图像。在默认情况下,原生相机在景观中。但是我不想使用位图。有可能这样做吗?请帮助。我是android开发的新手。

As my image is rotated by 90 degrees I need to rotate it using Bitmap before saving. But on some devices bmp is coming null.

当我的图像旋转90度时,我需要在保存之前使用位图旋转它。但是在某些设备上,bmp是零。

Camera Activity Layout

相机活动布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <FrameLayout
        android:id="@+id/camera_preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />
</RelativeLayout>

ManifestFile

ManifestFile

<activity
    android:name="com.example.androiddms.CameraActivity"
    android:configChanges="keyboardHidden|orientation"
    android:label="@string/title_activity_camera"
    android:screenOrientation="landscape"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
</activity>

Camera Activity.java

相机Activity.java

FileOutputStream fos = new FileOutputStream(pictureFile);
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
Matrix matrix = new Matrix();
matrix.postRotate(90);

bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
bmp.compress(Bitmap.CompressFormat.JPEG, 100,fos);

1 个解决方案

#1


0  

You should call onActivityResult after taking picture from camera and show your captured picture on ImageView. Bitmap takes memory but you should handle it. Follow the following steps below hopefully solve your Problem

您应该在从相机中拍照并在ImageView上显示您的捕获图片后调用onActivityResult。位图需要内存,但你应该处理它。按照下面的步骤,希望能解决你的问题。

STEP 1: open camera Intent and capture image from it:

第一步:打开相机的意图和捕捉图像:

private static final int REQUEST_CAMERA = 0;

Bitmap bmp;

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(intent, REQUEST_CAMERA);

STEP 2: override onActivityResult Method

步骤2:重写onActivityResult方法。

@Override   
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK)
        {
            if(requestCode == REQUEST_CAMERA)
            {               
                Uri uri = (Uri) data.getData();

                try 
                {
                    bmp = decodeUri(uri);

                    your_image_view.setImageBitmap(bmp);
                } 
                catch (FileNotFoundException e) 
                {
                    e.printStackTrace();
                }
            }

        }
    }

STEP 3: copy below method and paste it in your Activity

步骤三:复制下面的方法并粘贴到你的活动中。

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException 
    {
        BitmapFactory.Options o = new BitmapFactory.Options();

        o.inJustDecodeBounds = true;

        BitmapFactory.decodeStream(
                getContentResolver().openInputStream(selectedImage), null, o);

        final int REQUIRED_SIZE = 70;

        int width_tmp = o.outWidth, height_tmp = o.outHeight;

        int scale = 1;

        while (true) 
        {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) 
            {
                break;
            }
            width_tmp /= 2;

            height_tmp /= 2;

            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();

        o2.inSampleSize = scale;

        return BitmapFactory.decodeStream(
                getContentResolver().openInputStream(selectedImage), null, o2);
    }

and you are done after following these steps you will be able to capture image from camera and set it to your imageview without having memory error. Hope this will help you.

在完成这些步骤之后,您将能够从相机捕获图像并将其设置为您的imageview,而不会出现内存错误。希望这对你有帮助。

#1


0  

You should call onActivityResult after taking picture from camera and show your captured picture on ImageView. Bitmap takes memory but you should handle it. Follow the following steps below hopefully solve your Problem

您应该在从相机中拍照并在ImageView上显示您的捕获图片后调用onActivityResult。位图需要内存,但你应该处理它。按照下面的步骤,希望能解决你的问题。

STEP 1: open camera Intent and capture image from it:

第一步:打开相机的意图和捕捉图像:

private static final int REQUEST_CAMERA = 0;

Bitmap bmp;

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(intent, REQUEST_CAMERA);

STEP 2: override onActivityResult Method

步骤2:重写onActivityResult方法。

@Override   
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK)
        {
            if(requestCode == REQUEST_CAMERA)
            {               
                Uri uri = (Uri) data.getData();

                try 
                {
                    bmp = decodeUri(uri);

                    your_image_view.setImageBitmap(bmp);
                } 
                catch (FileNotFoundException e) 
                {
                    e.printStackTrace();
                }
            }

        }
    }

STEP 3: copy below method and paste it in your Activity

步骤三:复制下面的方法并粘贴到你的活动中。

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException 
    {
        BitmapFactory.Options o = new BitmapFactory.Options();

        o.inJustDecodeBounds = true;

        BitmapFactory.decodeStream(
                getContentResolver().openInputStream(selectedImage), null, o);

        final int REQUIRED_SIZE = 70;

        int width_tmp = o.outWidth, height_tmp = o.outHeight;

        int scale = 1;

        while (true) 
        {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) 
            {
                break;
            }
            width_tmp /= 2;

            height_tmp /= 2;

            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();

        o2.inSampleSize = scale;

        return BitmapFactory.decodeStream(
                getContentResolver().openInputStream(selectedImage), null, o2);
    }

and you are done after following these steps you will be able to capture image from camera and set it to your imageview without having memory error. Hope this will help you.

在完成这些步骤之后,您将能够从相机捕获图像并将其设置为您的imageview,而不会出现内存错误。希望这对你有帮助。