Custom camera function in camera API

A few days ago, the project needs to use the camera API to define the camera, and the previously used QR code also needs to write the underlying code, so summarize some matters about using the camera API. Now, because the official documents of jdk7.0 and above no longer recommend the use of camera package, but Camera2 package, but this time I'll talk about the use of camera first, and Camera2 will talk about it later.

The first is to add camera permissions. Camera hardware permissions and usage functions must be added in the list file, and the functions can be selectively placed according to the needs of the project.

For permissions and corresponding functions, please refer to the article http://www.cnblogs.com/BobGo/articles/5646751.html ;

I need two functions in the project. One is to add an ImageView layer on the surfaceview of the display camera, which can draw different mask layers on the ImageView; Another function is to display and save pictures after the camera takes photos.

The first is the simple use of the camera. Here, you may encounter the deformation of the camera preview in surfaceview. The solutions will be mentioned below:

Implement the surfaceholder. Callback interface in the current class and override the three methods surfacecreated(), surfacechanged(), surfaceregistry(). These three methods are the corresponding life cycle of surfaceview;

In surfacecreated(), execute camera. Open() to return a camera object and open the camera hardware. In surfacestudio(), the camera object calls release() to release the camera; In surfacechanged(), set the camera parameters. Getbestsize() is the most appropriate size for determining the camera hardware. This algorithm is officially provided. I did not use this algorithm before, but directly set the size, resulting in the deformation of the image displayed by surfaceview when the test machine is running.

You can also set functions such as flash in surfacechanged():

Finally, the takePicture (Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback) function is used to complete the photo shoot in the click monitoring of the camera. This function can have four callback interfaces. ShutterCallback is the callback pressed by the shutter. Here we can play the "click" sound and so on, and there are three PictureCallback interfaces behind. Corresponding to three pieces of image data respectively: original image, scaled and compressed image and JPG image. The image data can be obtained in void onpicturetaken (byte [] data, camera camera) of picturecallback interface. The corresponding three callbacks of the three pieces of data are called in the order of parameters. Generally, we only care about JPG image data, At this time, the first two picturecallback interface parameters can be directly passed to null;

Every time we call takepicture() to get an image, the camera will stop previewing. If we need to continue taking pictures, we need to call startpreview() function again at the end of onpicturetake() function of picturecallback above; When there is no need to take pictures, the camera object needs to actively call stoppreview() to stop the preview function;

The second function is to display and save the captured pictures. There may be two problems in this function: one is to display the captured pictures horizontally, and the other is to save the ImageView mask layer above the upper surfaceview to the user's mobile phone to achieve an effect similar to image synthesis;

The problem about the horizontal display of pictures is that Android officials believe that the horizontal screen of the mobile phone is the correct way to open the camera, so the saved pictures are also saved in the horizontal screen. Therefore, if you want to display the pictures in the vertical screen, you should rotate the pictures 90 degrees clockwise and adjust them to the vertical screen display mode. The specific code is as follows:

Another problem is to save the contents of two controls at the same time. In fact, the method is very simple. Customize a sub layout of FrameLayout and add only one method to the layout

Then use the XML layout used above as the parent control to save the image at the same time.

Finally, where the composite image needs to be saved in Java code, you only need to instantiate the just customized FrameLayout object and call getbitmap() to return the composite bitmap object;

So our function can be fully realized.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>