Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Camera2 to collect camera Raw data in Android

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to use Camera2 to collect camera raw data in Android". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Camera2 to collect camera raw data in Android" can help you solve the problem.

Demand analysis

In fact, the main purpose is to obtain the raw data of the camera without previewing, for the sake of flexibility and convenience to turn the preview on and off at any time, and to directly process the data in the future. the third is to make some preparations for other program development. So implement the following functions:

Get the camera data and manually draw the image, turn preview on / off and save the current camera image at any time, even if the Android Camera2 interface is off

After consulting some materials, the Android Java layer has abandoned the original Camera interface since it started with API21, so the Camera2 interface is used here. Compared with the Camera interface, the second-generation camera interface is much more complex, but more flexible, through the Google Camera2Basic example can clearly understand how to use. Here, the order of calling Camera2 in the CamCap program is as follows:

Like other programs, the data transmitted by CameraCaptureSession is obtained through ImageReader. Unlike the Google example, I canceled the transfer of TextureView and changed it to ImageReader alone to get the image stream, and set it to YUV_420_888 format to get the original data.

Turn on the camera.

After the camera is turned on, create a conversation

Call libyuv to do data conversion between RGB

After obtaining the YUV data, you can draw it on the UI interface. Through a simple understanding, you can draw through OpenGLES, or you can convert it to Bitmap to draw directly on TextureView. The latter is chosen here for simplicity. However, later found that android.graphics.Bitmap does not support the direct storage of YUV data, can only be converted to RGB data format, can be stored in Bitmap, and then drawn on TextureView. YUV to RGB, has been applied on C++ many times before, you can modify the existing code to run in java, but for performance reasons, decided to use libyuv. Libyuv is a very high-performance open source project based on CCompact +, which specializes in converting YUV to RGB formats.

To use libyuv, you need to cross-compile through NDK and call it through JNI. Libyuv is also easy to compile, first download the libyuv source code, the code address is: https://chromium.googlesource.com/libyuv/libyuv. Then make sure that NDK is installed (this can be installed directly in AndroidStudio), and then add the NDK directory to the environment variable. Finally, go to the libyuv directory and call ndk-build. Android.mk is already written in the libyuv project, so just compile it (I'm on Windows).

Be careful! When compiling, there is a problem that the JPEG library is not specified. If you do not want to rely on libjpeg, you can modify the Android.mk and delete the relevant compilation items of the JPEG library.

Establish a C++ file on AndroidStudio, encapsulate the libyuv interface, then expose the interface according to the JNI specification, and encapsulate the class in the Java layer to call the native method.

Draw an image

When drawing an image, there is a pit, that is, the rotation of the image. This is because the visual field coordinates of the camera sensor on the mobile phone are generally rotated by 90 or 270 degrees, so the picture captured by the camera needs to be rotated to restore the correct visual field. The rotation direction of the sensor is obtained by the following values

CameraManager.getCameraCharacteristics (camid) .get (CameraCharacteristics.SENSOR_ORIENTATION)

Based on this value, the build Matrix rotates the Bitmap

The Matrix build code is as follows:

This is the end of the content about "how to use Camera2 to collect camera raw data in Android". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report