Method of calling system camera to take pictures and take pictures in Android

preface

In many scenes, cameras are needed to take photos or videos and process them on the basis of photos or videos. However, the Android system source code is open source, which can be used by many device manufacturers, and customization is chaotic. Generally speaking, when the camera is needed to take pictures or take pictures, we will directly call the existing camera application of the system to take pictures or take pictures. We only take the shooting results for processing, so as to avoid some details of cameras of different devices. This blog will introduce how to call the existing camera application of the system to take photos and short films in Android applications, and process them. Finally, a simple demo will be used to demonstrate the effect.

1. System calls to existing camera applications

How to call the existing application of the system has been explained before, and it will be briefly repeated here. In developing applications, calling the existing application of the system requires using Intent to specify the Action and Category of the application, then opening the specified Activity by startActivity (Intent) or startActivityForResult (Intent, int). If the startActivityForResult () method is used to open and need to return the value, then rewrite the onActivityResult (startActivityForResult, t, t).

Let's take a look at the activities defined in the androidmanifest.xml manifest file of the existing camera application of the system:

It defines two activities, com.android.camera.camera for camera and com.android.camera.videocamera for camera. It can be seen from the literal meaning that in order to capture the data returned by the system camera, you generally need to use the following two actions to start the camera and the camera:

The above two parameters are defined in the form of static constants in the mediastore class: mediastore.action_ IMAGE_ Capture (camera) and mediastore.action_ VIDEO_ Capture.

2. The system takes photos with an existing camera

As mentioned above, mediastore.action is required to open the existing camera application of the system to take photos_ IMAGE_ Capture is the action of intent, and you can start the activity. However, when using the existing camera of the system, the picture will be saved to the directory of the system picture library by default. If you need to specify the saving path of the picture file, you need to set it in intent.

To set the saving path of the photos taken by the existing camera application of the system, you need to use the intent. Putextra() method through mediastore.extra_ Output to set the additional data of intent. Here, a URI parameter is passed, which can be the URI of a file path.

3. Get pictures taken by the existing camera of the system

In the newly opened activity, if you need to obtain its return value, you need to use the startactivityforresult (intent, int) method to start the activity and override onactivityresult (int, intent) to obtain the return data of the system camera. Then we only need to obtain the return value in onactivityresult().

If no path is specified, the photos taken by the system camera will be saved in the system default folder, which can be obtained by using the intent. Getextra() method. The obtained URI address represents the address of a content provider. If via mediastore.extra_ Output specifies the save path, so the address obtained through intent. Getextra () will be an empty address, but since it is the address specified by us, we don't worry about not finding it.

4. Demo system takes pictures with an existing camera

The above explains how to use the system camera to take photos and obtain the contents involved in the developed application. The following is a simple demo. In the demo, there are two buttons to start the system camera by specifying a path and not specifying a path, and obtain the return value and display it in the ImageView. The comments in the demo are more detailed and will not be repeated here.

Layout code: activity_ syscamera.xml

Implementation code: syscameraactivity.java

Effect display:

Here is a simple demonstration of how to call the existing camera application of the system to obtain the captured pictures without recycling the picture resources, so there may be a memory overflow error. Just restart the application.

5. The system takes video with an existing camera

The process of acquiring captured video from the existing camera application of the system is roughly the same as that of acquiring captured pictures, but it can set mediastore.extra through putextra()_ In addition to the output path, other values can be set. Here is a brief introduction:

6. Demo video capture system with existing camera

Since it is the same as the process of taking photos, I won't repeat it here. Go directly to the demo. In the demo, start a system through a button, shoot video with an existing camera, and finally save it on the SD card.

Implementation code:

Effect display:

Source code download: Android calls the system camera to take pictures and take pictures

summary

This explains how to use the existing camera application of the system to take photos and videos. In non camera related projects, if you need to take photos, you usually call the existing camera application of the system instead of directly calling camera hardware to obtain images.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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