无需使用位图即可旋转并保存捕获的图像

时间:2014-06-06 05:54:47

标签: android bitmap android-camera android-canvas

我希望使用Android 原生相机 旋转保存我的捕获图像到SD卡。默认情况下,本机相机位于横向中。但我不想使用位图。有可能这样做吗?请帮忙。我是android开发的新手。

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

相机活动布局

<?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>

manifest资源配置文件

<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

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 个答案:

答案 0 :(得分:0)

从相机拍摄照片后,您应致电onActivityResult,并在ImageView上显示拍摄的照片。 Bitmap占用记忆但你应该处理它。请按照以下步骤解决您的问题

第1步:打开camera Intent并从中捕获图片:

private static final int REQUEST_CAMERA = 0;

Bitmap bmp;

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(intent, REQUEST_CAMERA);

第2步: override 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();
                }
            }

        }
    }

第3步:复制下面的方法并将其粘贴到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);
    }

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

相关问题