【问题标题】:Rotate and Save captured image without using bitmap在不使用位图的情况下旋转并保存捕获的图像
【发布时间】:2014-06-06 05:54:46
【问题描述】:

我想旋转保存我使用 Android Native Camera 拍摄的图像到 sdcard。因为原生相机默认为横向。但是我不想使用位图。有可能这样做吗?请帮忙。我是安卓开发新手。

由于我的图像旋转了 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>

清单文件

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

【问题讨论】:

  • 位图遇到什么问题?
  • 位图占用大量内存。尽管存在一些技术来处理它们。但我只是想试试是否可以在不使用 Bitmaps 的情况下保存 Captured Image

标签: android bitmap android-camera android-canvas


【解决方案1】:

您应该在从相机拍照后致电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);
    }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多