1、AnativeWindow 官方解释为:
- ANativeWindow represents the producer end of an image queue.
- It is the C counterpart of the android.view.Surface object in Java,
- and can be converted both ways. Depending on the consumer, images
- submitted to ANativeWindow can be shown on the display or sent to
- other consumers, such as video encoders.
谷歌翻译:ANativeWindow表示图像队列的生产者端。它是Java中android.view.Surface对象的C对应物,并且可以双向转换。 取决于消费者,提交到ANativeWindow的图像可以显示在显示器上或发送给其他消费者,例如视频编码器。
因此可以理解的是 Java中可以提供一个surface通过NDK的方式利用AnativeWindow的API实现图像的绘制。
2、常规的相机数据处理并显示的方案:
①、利用OpenGL ES 中的shader语言对图像进行处理;(高效也推荐这个方法)
②、将camera的数据传到底层 进行识别,上层再利用cavans或者其它去绘制我们想表达的东西,例如:框框、文字等。
③、利用AnativeWindow API去实现相机图像数据的处理,这种做法存在一定的局限性,对于一些本身处理比较复杂的算法,图像的显示和绘制会极为的卡顿。但是AnativeWindow常常用于Video的显示,算是Android多媒体中一个比较有用的API。
3、AnativeWindow显示流程:
①、Camera2 通过ImageReader方式得到相机data;
②、将所得到的Yuvdata通过JNI传到native层;
③、native中将Yuvdata进行解码转成AnativeWindow可以用来绘制的图像格式;
AnativeWindow所支持的图像数据格式:
enum ANativeWindow_LegacyFormat {
// NOTE: these values must match the values from graphics/common/x.x/types.hal
/** Red: 8 bits, Green: 8 bits, Blue: 8 bits, Alpha: 8 bits. **/
WINDOW_FORMAT_RGBA_8888 = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
/** Red: 8 bits, Green: 8 bits, Blue: 8 bits, Unused: 8 bits. **/
WINDOW_FORMAT_RGBX_8888 = AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM,
/** Red: 5 bits, Green: 6 bits, Blue: 5 bits. **/
WINDOW_FORMAT_RGB_565 = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM,
};
4、关键代码
Java:
private ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
/*
* The following method will be called every time an image is ready
* be sure to use method acquireNextImage() and then close(), otherwise, the display may STOP
*/
@Override
public void onImageAvailable(ImageReader reader) {
// get the newest frame
Image image = reader.acquireNextImage();
if (image == null) {
return;
}
final byte[] yuvdata=ImageUtil.getBytesFromImageAsType(image,0);
draw(image.getWidth(),image.getHeight(),yuvdata,surface);
Log.d("pctest","yuvdata length:"+yuvdata.length+"image height:"+image.getHeight()+"image width"+image.getWidth());
image.close();
}
};
private void startPreview(CameraDevice mCamera) throws CameraAccessException {
SurfaceTexture texture = mPreviewView.getSurfaceTexture();
texture.setDefaultBufferSize(mPreviewSize.getWidth(),mPreviewSize.getHeight());
surface = new Surface(texture);
try {
// to set request for PREVIEW
mPreviewBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
} catch (CameraAccessException e) {
e.printStackTrace();
}
mImageReader = ImageReader.newInstance(mImageWidth,mImageHeight, ImageFormat.YUV_420_888,2);
mImageReader.setOnImageAvailableListener(mOnImageAvailableListener,mHandler);
mPreviewBuilder.addTarget(mImageReader.getSurface());
List<Surface> outputSurfaces = new ArrayList<>();
outputSurfaces.add(mImageReader.getSurface());
mCamera.createCaptureSession(outputSurfaces, mSessionStateCallback, mHandler);
}
Native:
在对数据处理中利用的 OpenCV 进行图像数据转码,只是为了方便= 。=(PS:转码这是太。。。)
extern "C"
JNIEXPORT void JNICALL
Java_com_example_anativewindow4camera2_MainActivity_draw(JNIEnv *env, jobject thiz, jint width,
jint height, jbyteArray yuvdata,
jobject surface) {
// TODO: implement draw()
jbyte *data = env->GetByteArrayElements(yuvdata, NULL);
Mat src(height + height / 2, width, CV_8UC1, data);
cvtColor(src, src, COLOR_YUV2RGBA_IYUV);
cv::transpose(src,src);
cv::flip(src,src,1);
ANativeWindow * window = ANativeWindow_fromSurface(env, surface);
ANativeWindow_acquire(window);
ANativeWindow_Buffer buffer;
ANativeWindow_setBuffersGeometry(window, src.cols, src.rows, WINDOW_FORMAT_RGBA_8888);
if (int32_t err = ANativeWindow_lock(window, &buffer, NULL)) {
LOGE("ANativeWindow_lock failed with error code: %d\n", err);
ANativeWindow_release(window);
}
uint8_t * outPtr = reinterpret_cast<uint8_t *>(buffer.bits);
int dst_line_size = buffer.stride * 4;
//一行一行拷贝
for (int i = 0; i < buffer.height; ++i) {
//void *memcpy(void *dest, const void *src, size_t n);
//从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中
memcpy(outPtr + i * dst_line_size,
src.data + i * src.cols * 4, dst_line_size);
}
ANativeWindow_unlockAndPost(window);
ANativeWindow_release(window);
src.release();
env->ReleaseByteArrayElements(yuvdata, data, 0);
}
5、记录一下,怕后面忘了。。。