8

我正在制作可以合成相机预览和png的相机应用程序。

我想将图像保存在 onPictureTaken 回调中

我的来源在下面

    public void onPictureTaken(byte[] data, Camera camera) {

            //preview from camera
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 

            // overlay image 
            Bitmap overlayBmp = overlay.getDrawingCache(); 

            //blank beatmap 
            Bitmap blankBitmap = 
            Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), 
            Bitmap.Config.ARGB_8888); 

            //make canvas
            Canvas canvas = new Canvas(blankBitmap); 

            //composite image
            canvas.drawBitmap(bitmap, null,new Rect(0, 0, bitmap.getWidth(), 
    bitmap.getHeight()), null); 
            canvas.drawBitmap(overlayBmp, null,new Rect(0, 0, bitmap.getWidth(), 
    bitmap.getHeight()), null); 

但是它显示内存不足错误,我该如何解决这个问题..?

05-20 15:13:49.114: E/AndroidRuntime(31647): FATAL EXCEPTION: main
05-20 15:13:49.114: E/AndroidRuntime(31647): java.lang.OutOfMemoryError
05-20 15:13:49.114: E/AndroidRuntime(31647):    at android.graphics.Bitmap.nativeCreate(Native Method)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at android.graphics.Bitmap.createBitmap(Bitmap.java:585)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at com.example.firstcameraappli.MainActivity$2.onPictureTaken(MainActivity.java:103)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:750)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at      android.os.Looper.loop(Looper.java:137)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at android.app.ActivityThread.main(ActivityThread.java:4514)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at java.lang.reflect.Method.invokeNative(Native Method)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at java.lang.reflect.Method.invoke(Method.java:511)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
4

3 回答 3

15

试试这个代码......

PictureCallback myPictureCallback_JPG = new PictureCallback()
{

    @Override
    public void onPictureTaken(byte[] data, Camera arg1) {

        BitmapFactory.Options opt;

        opt = new BitmapFactory.Options();
        opt.inTempStorage = new byte[16 * 1024];
        Parameters parameters = arg1.getParameters();
        Size size = parameters.getPictureSize();

        int height11 = size.height;
        int width11 = size.width;
        float mb = (width11 * height11) / 1024000;

        if (mb > 4f)
            opt.inSampleSize = 4;
        else if (mb > 3f)
            opt.inSampleSize = 2;

        //preview from camera
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,opt); 


    }
}

在位图中获取图像,然后根据您的要求使用。

于 2013-05-20T06:36:55.790 回答
0

这是 Android 上的一个众所周知的问题。问题是处理位图需要比大多数设备更多的内存。但是,您可以使用某些技巧。一些可以帮助您的讨论和解决方案:

于 2013-05-20T06:30:43.150 回答
0

如果要将图像保存到 SD 卡,请尝试这样

public void onPictureTaken(byte[] data, Camera camera) {
            camera.startPreview();
            FileOutputStream outStream = null;
            try {
                outStream = new FileOutputStream(
                        "/mnt/sdcard/mypicture.png");
                outStream.write(data);
                outStream.close();
                Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
            } catch (FileNotFoundException e) {
                e.printStackTrace();

            } catch (IOException e) {
                e.printStackTrace();
            } finally {

            }

        }
于 2013-05-20T06:35:12.220 回答