Camera development: 1. Camera Interface Design: 1. camera interface requirements:
① The camera screen is displayed on a horizontal screen: android: screenOrientation of the activity element in the list file
Attribute specifies the display status, landscape is displayed on a horizontal screen, and portrait is displayed on a vertical screen. If this attribute is not specified, the screen can be switched.
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
Android: screenOrientation = "landscape"
Android: name = ". DemoActivity"
Android: label = "@ string/app_name">
② Full screen display on the camera: the status bar and title bar of the activity are hidden.
In the onCreate method of the activity, use the requestWindowFeature () method to set the status of the display form, which is Window. FEATURE_NO_TITLE with no title.
Note: you must write it before the setcontentview () method.
Public class DemoActivity extends Activity implementsOnClickListener {
Public voidonCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Do not display the title:
// Be sure to write it before setcontentview
RequestWindowFeature (Window. FEATURE_NO_TITLE );
SetContentView (R. layout. main );
// Set the form to hide the status bar
GetWindow (). setFlags (
WindowManager. LayoutParams. FLAG_FULLSCREEN,
WindowManager. LayoutParams. FLAG_FULLSCREEN );
}
}
2. Camera UI layout:
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
Android: id = "@ + id/sv"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentBottom = "true"
Android: layout_alignParentRight = "true"
Android: orientation = "horizontal">
Android: id = "@ + id/autofocus"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Auto Focus"/>
Android: id = "@ + id/takepic"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "photo"/>
Ii. Development code:
// Obtain each component
SurfaceView sv = (SurfaceView) this. findViewById (R. id. sv );
// Focus button
Button autofocus = (Button) this. findViewById (R. id. autofocus );
// Take a photo
Button takepic = (Button) this. findViewById (R. id. takepic );
// Click the button to register the click event:
Autofocus. setOnClickListener (this );
Takepic. setOnClickListener (this );
// Obtain the SurfaceHolder object
SurfaceHolder holder = sv. getHolder ();
// Note: before creating holder:
Holder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS );
// Set the SurfaceHolder callback function
Holder. addCallback (new Callback (){
// Method of SurfaceHolder destruction
Public voidsurfaceDestroyed (SurfaceHolder holder ){
Camera. release (); // release resources
Camera = null;
}
// Method called when SurfaceHolder is created:
Public voidsurfaceCreated (SurfaceHolder holder ){
// Set the SurfaceHolder object to the member variable.
DemoActivity. this. holder = holder;
// Camera object
Camera = Camera. open ();
// Obtain the key-value pairs of all parameters of the camera.
// String parrten = camera. getParameters (). flatten ();
// TODO: set the camera parameters
// Obtain the Camera Parameter object:
Camera. Parameters parms = camera. getParameters ();
System. out. println (parms. flatten ());
// Set the value for the parameter:
Parms. setPreviewFrameRate (5); // Number of frames per second
Parms. setPictureSize (1280,960); // set the width and height.
Parms. setPictureFormat (PixelFormat. JPEG); // set the photo output format
Parms. set ("jpeg-quality", 100); // photo quality
//
// Camera. setParameters (parms );
// Set the preview interface. surfaceHolder is used as the parameter:
Camera. setPreviewDisplay (holder );
// Display the screenshot through SurfaceView
Camera. startPreview (); // start previewing
}
@ Override
Public voidsurfaceChanged (SurfaceHolder holder, int format,
Int width, int height ){
}
});
Button click event method:
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. autofocus: // click the focus button.
Camera. autoFocus (null); // Method for focusing the phone:
Break;
Case R. id. takepic: // click the camera button:
// Parameter 1 of the takePicture method: the callback when the camera sounds when the shutter is pressed.
Parameter 2: callback of the uncompressed image after the photo
Parameter 3: callback for compressing a photo into a jpeg Image
Camera. takePicture (null, null, new PictureCallback (){
// Method parameter 1: the binary byte array of the photo
Public voidonPictureTaken (byte [] data, Camera camera ){
// Determine whether sd is available
If (Environment. MEDIA_MOUNTED.equals (
Environment. getExternalStorageState ())){
// TODO: Save the image to the SD card.
File file = new File (
Environment. getExternalStorageDirectory (),
System. currentTimeMillis () + ". jpg ");
FileOutputStream fos = new FileOutputStream (file );
// Fos. write (data );
// Fos. flush ();
// Fos. close ();
// DecodeByteArray () method to obtain the Bitmap object
Bitmap bm = BitmapFactory. decodeByteArray (data, 0, data. length );
// Compression method:
Parameter 1: The compressed image format.
Parameter 2: file Quality
Parameter 3: output stream of the file
Bm. compress (CompressFormat. JPEG, 50, fos );
Fos. flush ();
Fos. close ();
Toast. makeText (
GetApplicationContext (), "saved image succeeded", 1). show ();
Camera. startPreview (); // preview the image again
} Else {
Toast. makeText (getApplicationContext (), "SD card unavailable", 1). show ();
}
}
});
Break;
}
}
Video Recording Development: mediaRecorder Class I. UI layout:
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
Android: id = "@ + id/sv"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentBottom = "true"
Android: layout_alignParentRight = "true"
Android: orientation = "horizontal">
Android: id = "@ + id/recoder"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Recording"/>
Android: id = "@ + id/stop"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: enabled = "false"
Android: clickable = "false"
Android: text = "stop"/>
Ii. Business Code: activity Class,
Public class DemoActivity extends Activity implementsOnClickListener {
Private SurfaceView sv;
Private Buttonstart, stop;
Private SurfaceHolderholder;
Private MediaRecorderrecorder;
@ Override
Public voidonCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Obtain the component
Sv = (SurfaceView) this. findViewById (R. id. sv );
// Obtain surfaceHolder
Holder = sv. getHolder ();
Holder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS );
Start = (Button) this. findViewById (R. id. recoder );
Stop = (Button) this. findViewById (R. id. stop );
Start. setOnClickListener (this );
Stop. setOnClickListener (this );
// Callback of SurfaceHolder
Holder. addCallback (new Callback (){
@ Override
Public void surfaceDestroyed (SurfaceHolder holder ){
//
}
@ Override
Public void surfaceCreated (SurfaceHolder holder ){
DemoActivity. this. holder = holder;
// Create a MediaRecorder object
Recorder = newMediaRecorder ();
Recorder. reset ();
// Capture video from camera
Recorder. setVideoSource (MediaRecorder. VideoSource. CAMERA );
// Set the audio collection
Recorder. setAudioSource (MediaRecorder. AudioSource. MIC );
// Set the output format
Recorder. setOutputFormat (MediaRecorder. OutputFormat. THREE_GPP );
// Set the size
Recorder. setVideoSize (720,480); // TODO?
Recorder. setVideoFrameRate (3); // 3 frames per second
// Sets the video encoding method.
Recorder. setVideoEncoder (MediaRecorder. VideoEncoder. H263 );
Recorder. setAudioEncoder (MediaRecorder. AudioEncoder. AMR_NB );
// Determine whether sd is available if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED )){
// Create a video file object
File file = new File (
Environment. getExternalStorageDirectory (),
System. currentTimeMillis () + ". 3gp ");
// Video output file
Recorder. setOutputFile (file. getAbsolutePath ());
// Set display
Recorder. setPreviewDisplay (DemoActivity. this. holder. getSurface ());
} Else {
Toast. makeText (getApplicationContext (), "SD card unavailable", 1). show ();
}
}
@ Override
Public void surfaceChanged (SurfaceHolder holder, int format, intwidth,
Int height ){
// TODO Auto-generatedmethod stub
}
});
}
// Button clicking event Method
Public void onClick (Viewv ){
Switch (v. getId ()){
Case R. id. recoder: // recording button
Try {
Recorder. prepare (); // prepare for recording
} Catch (Exception e ){
Toast. makeText (getApplicationContext (), "Recording preparation failed", 0). show ();
E. printStackTrace ();
} // Expected preparation
Recorder. start ();
Stop. setEnabled (true );
Stop. setClickable (true );
Start. setEnabled (false );
Start. setClickable (false );
Break;
Case R. id. stop: // stop button
Recorder. stop ();
Recorder. release ();
Start. setEnabled (true );
Start. setClickable (true );
Stop. setEnabled (false );
Stop. setClickable (false );
Break;
}
}
}
Intention to enable system camera:
1. Activate the System camera component:
Public class DemoActivity extends Activity {
/** Called when theactivity is first created .*/
Private ImageView iv;
@ Override
Public voidonCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Iv = (ImageView) this. findViewById (R. id. iv );
}
// Event Method for clicking the button:
Public void click (Viewview ){
// Implicitly intends to obtain the System camera component
Intent intent = new Intent ();
Intent. setAction ("android. media. action. IMAGE_CAPTURE ");
Intent. addCategory ("android. intent. category. DEFAULT ");
StartActivityForResult (intent, 0); // you can call this operation to obtain the returned value.
}
// Enable the component in the form of a returned value. The trigger method is as follows:
Protected voidonActivityResult (int requestCode, int resultCode, Intent data ){
// TODO Auto-generated method stub
Super. onActivityResult (requestCode, resultCode, data );
// New Intent ("inline-data"). putExtra ("data", bitmap );
System. out. println ("get photo ");
If (data! = Null ){
Uri uri = data. getData ();
// Iv. setImageURI (uri );
// System. out. println (uri. toString ());
String str = data. getAction ();
If (str! = Null ){
System. out. println (str );
}
// View the source code to obtain the image
Bitmap bitmap = data. getParcelableExtra ("data ");
// Set the image to display in ImageView:
Iv. setImageBitmap (bitmap );
}
}
}
Ii. Activate the video recording component of the system:
Public class DemoActivity extends Activity {
/** Called when theactivity is first created .*/
Private ImageView iv;
@ Override
Public voidonCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Iv = (ImageView) this. findViewById (R. id. iv );
}
// Button clicking Method
Public void click (Viewview ){
// Implicitly activates the recorded video component.
Intent intent = new Intent ();
Intent. setAction ("android. media. action. VIDEO_CAPTURE ");
Intent. addCategory ("android. intent. category. DEFAULT ");
StartActivityForResult (intent, 0 );
}
@ Override
Protected voidonActivityResult (int requestCode, int resultCode, Intent data ){
// TODO Auto-generated method stub
Super. onActivityResult (requestCode, resultCode, data );
// New Intent ("inline-data"). putExtra ("data", bitmap );
System. out. println ("get photo ");
If (data! = Null ){
Uri uri = data. getData ();
Play the recorded video through the retrieved uri
}
}
}