Android Development notes with camera

In Android, there are generally two methods to take photos with a camera. One is to call the camera provided by the system, and the other is to write a camera interface by yourself.

We want to add the following permissions:

1. Call system camera

The main steps of calling the camera provided by the system are as follows:

(1) Path name for constructing picture storage

(2) Start camera activity with intent

(3) Write the captured picture to a file

(4) Display picture in mainactivity

First, construct the picture name:

Then, start the camera activity:

Finally, display the picture in mainactivity. At this time, we overload the onactivityresult () method to get the message returned by camera.

The complete code is:

2. Write a camera interface yourself

Write your own camera interface, mainly using surfaceview to display the camera picture. Then save the current screen through a button.

Similarly, we need to add camera and sdcard permissions:

First, we initialize the surfaceview and add a corresponding callback to the surfaceview:

When starting the camera, first open the camera connection, then output its image to surfaceview, and then start the camera preview to display the camera picture on surfaceview. The picture here is 90 degrees different from the actual picture, so we need to rotate the image 90 degrees to be consistent with the direction of the photographed object.

When you turn off the camera, just stop the preview and release the camera resources.

Finally, save the captured picture to the sdcard. We click button to capture the picture and call the camera. Takepicture() method. Its prototype is:

Among them, shutter is the callback at the moment of pressing the shutter, that is, the shuttercallback. Onshutter() method will be called at the moment of pressing the shutter. Raw is the callback of uncompressed image, that is, the picturecallback. Onpicturetake() method will be called when processing the original image data. JPEG is a callback when processing JPEG images, that is, this method will be called when we need to save image data in JPG format, picturecallback. Onpicturetaken(). Here we call this method to store jpg images on the sdcard.

In this way, we can preview the camera image with surfaceview, and click button to save the current preview to SD card.

The complete code is as follows:

The above is all the content about Android Development notes and camera photography. I hope you like it.

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
分享
二维码
< <上一篇
下一篇>>