问题描述
因此我已将此摄像机预览设置为Camera
,SurfaceView
和SurfaceHolder
.我还有一个ImageView
,我将在其中放置摄像机图像的修改版本,并希望每秒更新一次.
So I have this camera preview set up with Camera
, SurfaceView
and SurfaceHolder
.I have also an ImageView
where I will be putting a modified version of the camera image and I want this to update lets say once every second.
当我从"res"加载图像时,所有代码均已准备就绪,并且已经可以使用,但是我很难从相机读取图像数据.
All code is ready and already working when I load images from "res" but I have a really hard time reading the image data from the camera.
我已经尝试过以下操作:
I've tried following already:
-
为
MediaStore.ACTION_IMAGE_CAPTURE
创建intent
并开始onActivityResult
从(Bitmap)data.getExtras().get("data")
获取小缩略图(实际上对我来说足够了)问题是,这会打开相机应用程序,您需要手动"拍照.
Creating an
intent
forMediaStore.ACTION_IMAGE_CAPTURE
and starting anonActivityResult
getting a small thumbnail (enough for me actually) from(Bitmap)data.getExtras().get("data")
The problem is that this opens the camera App and you need to "manually" take a picture.
创建Camera.PreviewCallback
,获取YuvImage
,然后使用YuvImage.compressToJpeg(...)
将其转换为图像.
这里的问题是,无论何时何地放置 Camera.setPreviewCallbackWithBuffer(PreviewCallback)
,我都无法启动它.
Creating a Camera.PreviewCallback
, taking the YuvImage
, and converting it to an image using YuvImage.compressToJpeg(...)
.
The problem here is that I can't get it to start no matter when or where i put the Camera.setPreviewCallbackWithBuffer(PreviewCallback)
.
修改:使这项工作最好的方法是什么?我的意思是QR码阅读器必须连续从相机中读取图像数据,它们如何工作?
What is the best way to make this work? I mean QR-Code readers must read the image data out of the camera continuously, how do they work?
推荐答案
我去了第2个选项,终于使它起作用了.
I went for option number 2 and finally made it work.
使用了此回调,之前忘记了@Override
used this callback, forgot the @Override
before
private Camera.PreviewCallback previewCallback= new Camera.PreviewCallback()
{
@Override
public void onPreviewFrame(byte[] data,Camera cam)
{
Camera.Size previewSize = cam.getParameters().getPreviewSize();
YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21,previewSize.width,previewSize.height, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0,0,previewSize.width,previewSize.height),80,baos);
byte[] jdata = baos.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(jdata,0,jdata.length);
}
};
并使用setPreviewCallback
而不是setPreviewCallbackWithBuffer
SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback()
{
public void surfaceCreated(SurfaceHolder holder) {
camera.setPreviewCallback(previewCallback);
}
}
这篇关于从相机,SurfaceView或SurfaceHolder连续获取图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!