Android:访问硬件相机预览帧数据,无需绘制它们 [英] Android: Accessing hardware camera preview frame data without drawing them

查看:809
本文介绍了Android:访问硬件相机预览帧数据,无需绘制它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据来自Java SDK侧的android相机文档,必须向相机预览帧提供(可见和活动的)要绘制的表面以便访问帧数据。我已经链接了一些我在这里遇到的事情(我是新的,所以封顶在2个超链接),但我翻了大量的各种东西的文档,然后在SO上发布我自己的问题。



我的问题:



a)我明确不想将相机预览画到屏幕上,



b)是的,我看到了这个:
无预览的Android相机



但是,这表示使用这个库的应用程序必须将这个(表面上)任意视图插入到他们的应用程序布局中,它必须在应用程序生命周期中始终可见(它们不能切换布局,更改父容器的可见性或使用子活动)。



事实上,我的需求类似于这个海报的,除了我想要连续的实时流的相机预览数据,而不是捕获保存到磁盘上的图像。因此 PictureCallback 适用于他以及 myCamera.takePicture 调用。由于显而易见的原因,将连续捕获写入磁盘对我来说不是一个解决方案,所以在我的情况下不会工作。 myCamera.takePicture 也比获得预览帧慢得多。



c)我开始使用NDK ,并得到了一个很好的感觉。但是,根据通过本机访问摄像头数据



如果这已经过时,并且有固定的NDK路由从Android设备获取摄像机数据,我找不到他们,所以如果你能指出给我,这将是巨大的。



d)我想让这个库从Unity (以unity插件的形式),为此我想能够将其编译成一个JAR(或.so为原生),并期望它工作在Android应用程序导入库,以便他们可以使用



上下文:



我想要能够创建用于Android应用程序的视觉处理库,并且不想限制应用程序使用库必须使用特定的应用程序布局和绘制特定的视图到屏幕,以使用视觉处理结果 - 一个非常简单的例子,如果应用程序想要使用我的库来获取相机看到的平均颜色,并想要在屏幕上着色的图像。



无论建议,我可以得到任何点我会有超级有益的。非常感谢您的时间!

解决方案

我完全忘了我有这个问题。 2年和几个Android SDK版本后,我们有一个工作系统。



我们使用了扩展的 SurfaceTexture cameraSurface 参考所需的相机。使用cameraSurface的 SurfaceTexture ,我们调用

  mCamera.setPreviewTexture(mSurfaceTexture); 
mCamera.startPreview();

然后覆盖当前活动相机的 previewCallback 您的活动或您需要的地方。

  mCamera.setPreviewCallback(new PreviewCallback(){

public void onPreviewFrame(final byte [] data,final Camera camera){
//处理任何你需要的字节的内容
}
});

这样可以连续处理相机所看到的内容,而不仅仅是浏览存储的图像。当然,这将限制您的相机的预览分辨率(这可能不同于静态捕获或视频分辨率)。



如果我得到时间,我会尝试抛出一个准系统工作演示。



编辑:我链接到的SDK不再是免费的。您仍然可以通过 BitGym联系人页面申请一个。


According to the android camera docs from the Java SDK side, the camera preview frames have to be supplied a (visible and active) surface to be drawn to in order to access the frame data. I have linked a few of the things I have come across here (I'm new so capped at 2 hyperlinks), but I went over tons of documentation of various things before winding up posting my own question here on SO.

My Problems:

a) I explicitly don't want to draw the camera preview to the screen, I just want the byte data (which I can get from here) straight from the camera buffer if possible.

b) Yes, I saw this: Android Camera without Preview.

However, this dictates that apps using this library have to insert this (seemingly) arbitrary view into their app layout, which has to be visible all the time (they can't switch layouts, change visibility of parent containers or use a subactivity) during app life-cycle.

In fact, my needs are similar to this poster's, except I want continuous real-time stream of the camera preview data, not a capture saved to image on disk. Hence PictureCallback works for him together with the myCamera.takePicture call. For obvious reasons, writing continuous captures to disk is not a solution for me, so that won't work in my case. myCamera.takePicture is also much slower than getting the preview frames.

c) I have started dabbling with the NDK, and have gotten a pretty good feel for it. However, according to this accessing camera data via native is just not supported or recommended, and a huge hassle for device compatibility even then.

If this is outdated, and there are solid NDK routes to acquiring camera data from android devices, I couldn't find them, so if you could point them out to me that would be great.

d) I want to make this library accessible from tools like Unity (in the form of a unity plugin), for which I want to be able to just compile it into a JAR (or .so for native) and expect it to work on android apps that import the library that way so that they can use the camera without specific UI/layout configurations needed from the app developer.

Context:

I want to be able to create a vision-processing library for use in android apps, and don't want to limit the apps using the library to have to use specific app layouts and draw specific views to the screen in order to use the vision processing results - for a very simple example, if an app wants to use my library to get the average color of what the camera sees, and wants to tint an image on the screen that color.

Whatever suggestions I can get towards any of the points I have will be super-helpful. Thanks a lot for your time!

解决方案

I completely forgot I had this question up. 2 years and a couple of Android SDK versions later, we have a working system.

We're using an extended SurfaceTexture, cameraSurface with a reference to the required camera. Using cameraSurface's SurfaceTexture, we call

mCamera.setPreviewTexture(mSurfaceTexture);
mCamera.startPreview();

Then override the current active camera's previewCallback from your activity or wherever you need it.

mCamera.setPreviewCallback(new PreviewCallback() {

     public void onPreviewFrame(final byte[] data, final Camera camera) {
         // Process the contents of byte for whatever you need
     }
});

This allows you to continuously process what the camera sees, rather than just having to go through stored images. Of course, this will be limited to your camera's preview resolution (which may be different from the still capture or video resolutions).

If I get the time, I'll try to throw up a barebones working demo.

Edit: The SDK I had linked to is no longer freely available. You can still request one via the BitGym contact page.

这篇关于Android:访问硬件相机预览帧数据,无需绘制它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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