Android development: real-time processing of camera preview frame videos -- Analysis of the comprehensive application of previewcallback, onpreviewframe and asynctask

Source: Internet
Author: User

In many cases, the android camera module not only previews, but also takes photos easily. Instead, it needs to be able to detect videos, such as the most common face detection. Before you press the photo button, you can detect the face, mark it in a rectangle, and then press the photo button. So how to get a preview frame video?

You only need to inherit the previewcallback interface in the activity. Example:

Public class rectphoto extends activity implements surfaceholder. Callback, previewcallback {}. (Note that this surfaceholder. Callback is used to preview the camera video. For more information, see my post ).

After this method is inherited, this function is automatically reloaded: Public void onpreviewframe (byte [] data, camera) {}. The data in this function is a real-time preview of frame videos. Once the program calls the previewcallback interface, the onpreviewframe function is automatically called. There are three methods to call previewcallback. You can refer to the following three methods to call this callback. The so-called callback is to automatically call this function when the condition is met. They are:. setpreviewcallback,
Setoneshotpreviewcallback, setpreviewcallbackwithbuffer. I generally use the second method.

Here we will explain that if activity inherits the previewcallback interface, you only need camera. setoneshotpreviewcallback (this. The program automatically calls the onpreviewframe function in the main activity class. If the camera. setoneshotpreviewcallback () function is an internal class in the main class activity, such as class
In a, the parameter should be set to camera. setoneshotpreviewcallback (youractivity. This ). Of course, you can also define a variable here, such as camera. previewcallback mpreviewcallback. You can use camera. setoneshotpreviewcallback (mpreviewcallback) to complete the call. I believe many people are familiar with this.

It is reasonable to write your handler in the onpreviewframe () function. Generally, this is not the case because the algorithm for processing Real-Time preview frame videos may be complicated. This requires the use of asynctask to enable a thread to process data in the background. Assume that we define a facetask for face detection. You can write it as follows:

/* Customize the facetask class to enable a thread to analyze data */
Private class facetask extends asynctask <void, void, void> {

Private byte [] mdata;
// Constructor
Palmtask (byte [] data ){
This. mdata = data;
}

@ Override
Protected void doinbackground (void... Params ){
// Todo auto-generated method stub
Size size = mycamera. getparameters (). getpreviewsize (); // get the preview size
Final int W = size. width; // width
Final int H = size. height;
Final yuvimage image = new yuvimage (mdata, imageformat. nv21, W, H, null );
Bytearrayoutputstream OS = new bytearrayoutputstream (mdata. Length );
If (! Image. compresstojpeg (New rect (0, 0, W, h), 100, OS )){
Return NULL;
}
Byte [] TMP = OS. tobytearray ();
Bitmap BMP = bitmapfactory. decodebytearray (TMP, 0, TMP. Length );

Dosomethingneeded (BMP); // self-defined real-time analysis and preview frame video Algorithm

Return NULL;
}

}

Note that the above BMP is the real-time preview frame data in bitmap format.Dosomethingneeded (BMP) is the processing of the previewed frame video. It can be a face detection or other operations, such as analyzing whether there is a fire. Or transmission.
In addition, yuvimage and imageformat. nv21 are used to parse data. On the Huawei u9200, The android4.0.3 system runs well. The supported formats on different mobile phones may be different. You can also write algorithms for conversion on the Internet. You can find the conversion algorithm you need. However, if you support this format, you do not need to write the conversion algorithm yourself.

Onpreviewframe () can be written as follows:

/* Get the preview frame video */
Public void onpreviewframe (byte [] data, camera ){
// Todo auto-generated method stub
If (null! = Mfacetask ){
Switch (mfacetask. getstatus ()){
Case running:
Return;
Case pending:
Mfacetask. Cancel (false );
Break;
}
}
Mfacetask = new palmtask (data );
Mfacetask.exe cute (void) null );

}

The above mfacetask is a global variable. Through the comprehensive application of onpreviewframe and asynctask, the complex processing algorithm is executed in the background, that is, doinbackground. Is it relatively green?

The next step is when to trigger onpreviewframe (). In this function, you can press a button to trigger it once, and write mycamera In the listener. setoneshotpreviewcallback (rectphoto. this); it is automatically triggered once. Some people say they want to focus first and then analyze the preview frame. In onautofocus. As follows:

// Auto-focus variable callback
Myautofocuscallback = new autofocuscallback (){

Public void onautofocus (Boolean success, camera ){
// Todo auto-generated method stub
If (SUCCESS) // success indicates that the focus is successful.
{
Log. I (TAG, "myautofocuscallback: success ...");
Mycamera. setoneshotpreviewcallback (rectphoto. This );

}
Else
{
// No focus is set.

Log. I (TAG, "myautofocuscallback: failed ...");

// You can also add mycamera. autofocus (myautofocuscallback) here. If the focus fails, start the focus again.
}

}
};

Most of the time, you want the program to automatically check the preview frame at intervals. The implementation is as follows:

class ScanThread implements Runnable{public void run() {// TODO Auto-generated method stubwhile(!Thread.currentThread().isInterrupted()){try {if(null != myCamera && isPreview){    //myCamera.autoFocus(myAutoFocusCallback);myCamera.setOneShotPreviewCallback(RectPhoto.this);Log.i(tag, "setOneShotPreview...");}Thread.sleep(1500);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();Thread.currentThread().interrupt();}}}}

Enable scan thread in oncreate. New thread (New scanthread (). Start. If you want to manually trigger or abort this scanning activity, you can set a flag in the while loop in scanthread. For details, refer to my previous blog post.


The last note is that if previewcallback is added to the program, it is best to execute mycamera when surfacedestroy releases camera. setoneshotpreviewcallback (null); or mycamera. setpreviewcallback (null); abort this callback, and then release camera for security. Otherwise, an error may be reported.

Android enthusiasts are welcomed to join the group 248217350. Remarks: Yanzi

--------------------------------------------------------------------- This article is original. For details, see Author: yanzi1225627.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.