怎样有效地修改从相机捕获字节? [英] How do I efficiently modify captured bytes from the camera?

查看:129
本文介绍了怎样有效地修改从相机捕获字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我画从摄像机的图像上的重叠,并保存结果到文件。要做到这一点,我传递一个包含code下面takePicture()回调。对于较大的图像大小,我越来越崩溃,在方法的第一行OutOfMemoryError异常。

有没有什么办法可以更有效地做到这一点?看来,它不可能使从字节[],这立即双打我的内存使用量的可变位图。如果它不能在高分辨率的情况做这样子,怎么的可以的我产生大量拍摄的图像上叠加而不会耗尽内存?

 公共无效onPictureTaken(字节[] rawPlainImage,相机摄像头){
    位图plainImage = BitmapFactory.de codeByteArray的(rawPlainImage,0,rawPlainImage.length);
    plainImage = plainImage.copy(plainImage.getConfig(),TRUE);
    帆布combinedImage =新的Canvas(pl​​ainImage);
    combinedImage.drawBitmap(mOverlay,mOverlayTransformation,NULL);
    //写plainImage(今修改)到一个文件
    plainImage.recycle();
}


解决方案

您实际上并不需要取消code中的形象。相反,绘制overlay的画布,画布保存为位图,该位图转换为字节数组,然后结合画布的字节数组和位图,然后保存。

I'm drawing an overlay on an image from the camera and saving the result to a file. To do this, I am passing a callback containing the code below to takePicture(). With larger image sizes, I am getting crashes with an OutOfMemoryError at the first line of the method.

Is there any way I can do this more efficiently? It seems that it's not possible to make a mutable Bitmap from the byte[], which doubles my memory usage immediately. If it can't be done this way at high resolutions, how can I produce an overlay on a large captured image without running out of memory?

public void onPictureTaken(byte[] rawPlainImage, Camera camera) {
    Bitmap plainImage = BitmapFactory.decodeByteArray(rawPlainImage, 0, rawPlainImage.length);
    plainImage = plainImage.copy(plainImage.getConfig(), true);
    Canvas combinedImage = new Canvas(plainImage);
    combinedImage.drawBitmap(mOverlay, mOverlayTransformation, null);
    //Write plainImage (now modified) out to a file
    plainImage.recycle();
}

解决方案

You don't actually need to decode the image. Instead draw the overlay to the canvas, save the canvas as a bitmap, convert that bitmap to a byte array and then combine the byte array of the canvas and the bitmap and then save that.

这篇关于怎样有效地修改从相机捕获字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆