Android calls the front camera [original]

Source: Internet
Author: User

First of all, I want to vent my little dissatisfaction with Android. For Shenma 2.3, only APIs are available to support front camera. For Shenma 2.3 and earlier versions, there will also be front cameras, different methods are used to call the front camera for each Shenma manufacturer. Nima is also writingProgramAs for how many different versions have different compatibility issues. Biological recognition technology cannot afford to hurt you !!! Google, you can be unified !!! Don't torture our coomon. Damn soul vendors, you can do it in a rough way.

If (model. Equals (sumpers & HTC & Moto) {system. Exit (0)} // supports shanzhai.

Let's talk about how to record the frequency first. The simplest way is to call mediarecorder. It is best to open a subthread to write:

There are many posts on the internet saying that there are bugs and the problem lies in encoding. Because the video encoding supported by each vendor is a little different, don't count on the sameCodeSupports all mobile phones. I tested Ten mobile phones with h264,

9 are passed, and 1 is not supported by HTC wild fire. So use default. Mediarecorder. videoencoder. Default

Runnable run = new runnable (){
Public void run (){
Try {
Myrecaudiofile = new file (environment. getexternalstoragedirectory (), "video.mp4"); // create a temporary file
If (myrecaudiofile. exists ()){
Myrecaudiofile. Delete ();
}
Myrecaudiofile. createnewfile ();
Recorder. Reset ();
Recorder. setvideosource (mediarecorder. videosource. Camera); // video source
Recorder. setaudiosource (mediarecorder. audiosource. Mic); // The recording source is a microphone.
Recorder. setoutputformat (mediarecorder. outputformat. mpeg_4); // The output format is MP4.
Recorder. setvideoencoder (mediarecorder. videoencoder. Default); // Video Encoding
Recorder. setaudioencoder (mediarecorder. audioencoder. Default); // Audio Encoding
Recorder. setvideoframerate (15); // specifies the video frame frequency.
Recorder. setvideosize (320,240); // video size
Recorder. setpreviewdisplay (msurfaceholder. getsurface (); // Preview
// Recorder. setmaxduration (10000); // maximum duration
Recorder. setoutputfile (myrecaudiofile. getabsolutepath (); // save path
Recorder. Prepare ();
Recorder. Start ();
Mhandler. sendemptymessagedelayed (1, 10000 );
} Catch (exception e ){
Stop (true );
}
}
};

The problem of front camera

2.3 of API calls are very easy to come before.

Camera Android. Hardware. Camera. Open (INT cameraid)

Creates a new camera object to access a special hardware camera.

Cameraid The hardware camera to access, between 0 and Getnumberofcameras () -1.

If (build. version. sdk_int> = build. version_codes.gingerbread ){
For (INT I = 0; I <camera. getnumberofcameras (); I ++ ){
Camerainfo info = new camerainfo ();
Camera. getcamerainfo (I, Info );
If (info. Facing = camerainfo. camera_facing_front) {// This is the front camera.
C = camera. Open (I );
}
}
}
If (C = NULL ){
C = camera. open ();
}

C. Unlock (); // note that you must call unlock to obtain control of the camera before setting the mediarecorder parameter. Camera is a singleton. If no call is made, the program will be suspended.
Recorder. setcamera (C );

Void Android. Hardware. Camera. Unlock ()

Unlocks the camera to allow another process to access it. Normally, the camera is locked to the process with an active camera objectRelease ()Is called. To allow rapid handoff between processes, you can call this method to release the camera temporarily for another process to use; once the other process is done you can callReconnect ()To reclaim the camera.

This must be done before callingAndroid. Media. mediarecorder. setcamera (CAMERA).

If you are not recording video, you probably do not need this method.

In this way, we are done. Note that when you stop shooting or the program is down, call camera. Lock () to release control of camera. Otherwise, you cannot start camera. Knows to throw an exception.

Private void stop (Boolean force ){
If (! Force ){
Recorder. Stop ();
}
Recorder. Reset ();
Recorder. Release ();
Recorder = NULL;
If (C! = NULL ){
C. Lock ();
C. Release ();
C = NULL;
}
Setresult (force? Activity. result_canceled: activity. result_ OK );

// Remember, when the system's camera is called to take a photo, the URI of the file will be returned in the intent. You can also do intent. setdata (URI. fromfile (tempfile) in this way ))
This. Finish ();
}

Next, let's talk about how to call the prefix before 2.3. The company only has a front-end for HTC wild fire and sumsung i9000, so I only studied them. Other brands can be used in a similar way. If you get them out, please let me know, mark

Debug is an interesting thing. I mean a successful debug .....

When testing these two mobile phones, I read logs one by one line. Both eyes were spent, cursing HTC and Samsung.

06-23 17:49:05. 058: Warn/cameraservice (2374): getparameters (appshuttersound = 0; anti-shake = 0; antibanding = auto; antibanding-values = auto, 50Hz, 60Hz, off; beauty-shot = 0; blur = 0; camera-id = 1; chk_dataline = 0; contrast = 2; contrast-max = 4; contrast-min = 0; effect = none; effect-values = none, Mono, negative, sepia; exposure-compensation = 0; exposure-compensation-step = 0.5; Focal-length = 3.79; Focus-mode = auto; focus-mode-values = auto, macro; horizontal-View-angle = 51.2; ISO = auto; JPEG-quality = 100; JPEG-thumbnail-Height = 120; JPEG-thumbnail-quality = 100; JPEG-thumbnail-size-values = 160x160, 0x0; JPEG-thumbnail-width =; max-exposure-compensation = 4; max-zoom = 30; Metering = center; Min-exposure-compensation =-4; picture-format = JPEG; picture-format-values = JPEG; picture-size = 2560x1920; picture-size-values = 2560x1920,2048x1536,1600x1200,640x480,2560x1536,2048x1232,1600x960,800x480; preview-format = yuv420sp; preview-format-values = yuv420sp; preview-frame-rate = 30; preview-frame-rate-values = 15, 30; preview-size = 640x480; preview-size-values = 320x240,640x480,800x480; rotation = 0; saturation = 2; saturation-max = 4; saturation-min = 0; scene-mode = Au

This is Samsung's camera. Parameters. Which one do you think is the front camera? Camera-id = 1 bingo is it.

// Parameters = C. getparameters ();
// Parameters. Set ("camera-ID", 2); // my i9000 was successfully set to a front-end on a mobile phone before sumeda 2.3.

06-23 18:14:50. 697: Debug/cameraservice (1622): getparameters (make = HTC; antibanding = auto; antibanding-values = auto, 50Hz, 60Hz; brightness = 0; brightness-Def = 0; brightness-max = 20; brightness-min =-20; contrast = 0; contrast-Def = 0; contrast-max = 100; contrast-min =-100; device =; device-list =/dev/video0,/dev/video1,/dev/video2; effect = none; effect-values = none, mono, negative, Sepia, Aqua; exposure-compensation = 0; exposure-compensation-step = 0; fnumber = 3.5; Focal-length = 2.76; Focus-mode = auto; focus-mode-values = auto; GPS-mapdatum = WGS 84; horizontal-View-angle = 54; ISO = auto; JPEG-quality = 85; JPEG-thumbnail-Height = 240; JPEG-thumbnail-size-values = 320x320, 0x0; JPEG-thumbnail-width =; max-exposure-compensation = 0; max-zoom = 10; meter-mode = meter-average; Min-exposure-compensation = 0; Models = a3360; picture-format = JPEG; picture-format-values = JPEG; picture-size = 2048x1536; picture-size-values = 2048x1536,1600x768,800x600,640x480,384x288,352x288,320x240,176x144 x; preview-format = yuv420sp; preview-format-values = yuv420sp; preview-frame-rate = 15; preview-frame-rate-values = 30 ,15; preview-size = 320x240; previ

Okay, This Is HTC. Which one do you guess? Sorry, wood has this parameter. At that time, I was depressed. You can't afford to hurt Android programmers. A bunch of compatibility issues. Nima, please try it.

Well, let's look at the built-in camera. If you know how to observe, it's quite interesting. The rear camera of the wild fire is called (master). If you have this machine, you can test it on your own.

06-23 18:14:50. 716: Debug/cameraservice (1622): setparameters: video_input = Main (interesting)

Debug it. Is it interesting to get different keys by calling video_input Nima? It's so confusing and cool. If people are crazy about sex, they can write it from the bottom layer. Once again, it's okay. Yes. We have a lot of APIs that can be written in half an hour. debugging takes several days. Yes !!!

You can try again to switch the system's camera to the sub-camera... Secondary. Yes !!!

Okay. Video_input = secondary. Still not working... S lower case. Nima cannot afford to hurt.

// Parameters = C. getparameters ();

// Parameters. Set ("camera-ID", 2); // The front of the mobile phone before Samsung 2.3

// Parameters. Set ("camera-ID", 1); // the rear of the mobile phone before 2.3

// Parameters. Set ("video_input", "secondary"); // phone frontend before htc2.3

// Parameters. Set ("video_input", "Main"); // the rear of the phone before htc2.3

// C. setparameters (parameters );

OK. All of them are finished here. Debug. Android. A painful life. You can't afford to hurt codoon !!!!

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.