从显示实时摄像机SurfaceView获取RGB [英] Get RGB from a SurfaceView displaying Live camera
问题描述
我使用的显示实时摄像机 SurfaceView
camera.start preVIEW();
。我如何能获取实时RGB读数相机任何想法?
I am displaying a live camera in SurfaceView
using camera.startPreview();
. Any idea on how I can get live RGB readings from the camera?
感谢
推荐答案
我想我能得到从 SurfaceView
转换数据。但是,使用最好的方法是:
I thought I could get the data converted from the SurfaceView
. But the best method to use is :
- 设置相机的方向为90度。
- 设置输出格式为NV21(即<一href="http://developer.android.com/reference/android/hardware/Camera.Parameters.html#set$p$pviewFormat%28int%29"相对=nofollow>存在保证要在所有设备都支持)。
- 设置为打开闪光灯。
- 在开始preVIEW在
SurfaceView
。
- Set the camera's orientation to 90 degrees.
- Set output format to NV21 (which is guranteed to be supported on all devices).
- Set to turn the Flash ON.
- Start preview in the
SurfaceView
.
项目
camera = Camera.open();
cameraParam = camera.getParameters();
cameraParam.setPreviewFormat(ImageFormat.NV21);
camera.setDisplayOrientation(90);
camera.setParameters(cameraParam);
cameraParam = camera.getParameters();
camera.setPreviewDisplay(surfaceHolder);
cameraParam.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(cameraParam);
camera.startPreview();
然后,我称之为设置previewCallback
和在previewFrame
获得进入框架,并把它转换为RGB像素阵列。我可以再得到的图像中每种色彩的强度由通过为
循环运行myPixels阵列,并检查 Color.red平均所有像素的强度(myPixels [I])
为每个想要的颜色(上previewFrame的在
)。
Then, I call the setPreviewCallback
and onPreviewFrame
to get the incoming frame, and convert it to RGB pixel array. Which I can then get intensity of each color in the picture by averaging all pixels intensity by running myPixels array through a for
loop, and checking Color.red(myPixels[i])
for each desired color (inside the onPreviewFrame
).
camera.setPreviewCallback(new PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
int frameHeight = camera.getParameters().getPreviewSize().height;
int frameWidth = camera.getParameters().getPreviewSize().width;
// number of pixels//transforms NV21 pixel data into RGB pixels
int rgb[] = new int[frameWidth * frameHeight];
// convertion
int[] myPixels = decodeYUV420SP(rgb, data, frameWidth, frameHeight);
}
}
其中,德codeYUV420SP
找到的这里。
我计时这个操作需要大约200毫秒的每一帧。 有没有做这件事的一个更快的方法?的
I timed this operation to take about 200ms for each frame. Is there a faster way of doing it?
这篇关于从显示实时摄像机SurfaceView获取RGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!