The camera function of the Android system has been implemented in 2 ways, which can be used for reference:
1. Call the system camera to take a picture
First, find the Androidmanifest.xml file to add user rights
<uses-permission android:name= "Android.permission.CAMERA" ></uses-permission>
<uses-feature android:name= "Android.hardware.camera"/>
<uses-feature android:name= "Android.hardware.camera.autofocus"/>
Second, add 2 controls (button and ImageView) to the main class Java file, which is used to trigger the button event and display the picture, which is purely a personal hobby.
Final int take_picture = 1;//The camera that identifies your program in the return method
The key is here: Startactivityforresult (New Intent ("Android.media.action.IMAGE_CAPTURE"), take_picture);
is to open the system to bring your own camera, the following is the processing of the data taken, save the data
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
if (Requestcode = = take_picture) {
if (ResultCode = = RESULT_OK) {
Bitmap BM = (Bitmap) Data.getextras (). Get ("data");
Img.setimagebitmap (BM);//want the image to appear on the ImageView view, Private ImageView img;
File Mycapturefile = new file ("Sdcard/123456.jpg");
try {
Bufferedoutputstream BOS = new Bufferedoutputstream (new FileOutputStream (Mycapturefile));
/* Use the Compress-shift method */
Bm.compress (Bitmap.CompressFormat.JPEG, N, BOS);
/* Call the Flush () method to update Bufferstream */
Bos.flush ();
/* End OutputStream */
Bos.close ();
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
This will enable the call system comes with the camera, very simple operation.
2. Write your own program to save your photos
Photo Landscape file Lay.xml These definitions first.
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
>
<linearlayout
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:paddingleft= "130px"
android:paddingright= "200px"
>
<surfaceview
Android:id= "@+id/msurfaceview1"
android:visibility= "Visible"
Android:layout_width= "320px"
android:layout_height= "240px" >
</SurfaceView>
</LinearLayout>
</LinearLayout>
Where Surfaceview is used for previewing,
Initialize a series of values in the Oncreat function:
Requestwindowfeature (Window.feature_no_title);
Setcontentview (R.layout.lay);
/* Get screen resolution pixels */
Displaymetrics dm = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
mImageView01 = (ImageView) Findviewbyid (R.ID.MYIMAGEVIEW1);
/* Use Surfaceview as the camera preview * *
mSurfaceView01 = (Surfaceview) Findviewbyid (R.ID.MSURFACEVIEW1);
/* bind Surfaceview, get surfaceholder Object */
MSurfaceHolder01 = Msurfaceview01.getholder ();
/* Activity must implement Surfaceholder.callback */
Msurfaceholder01.addcallback (Takephoto.this);
/*
* with Surface_type_push_buffers (3)
* As Surfaceholder Display type
* */
Msurfaceholder01.settype
(surfaceholder.surface_type_push_buffers);
The function and parameter settings of the camera are initialized first:
Private Camera mCamera01;
MCAMERA01 = Camera.open ();
/* Create Camera.parameters Object */
Camera.parameters Parameters = Mcamera01.getparameters ();
/* Set photo format to JPEG */
Parameters.setpictureformat (Pixelformat.jpeg);
LOG.I (TAG, "pic is jpeg");
/* Specify the screen size of preview */
Parameters.setpreviewsize (320, 240);
LOG.I (TAG, "pic Pingmu fenbianlv");
/* Set the picture resolution size */
Parameters.setpicturesize (1024, 768);
LOG.I (TAG, "pic Tupian fenbianlv");
/* Set Camera.parameters to camera */
Mcamera01.setparameters (parameters);
/* Setpreviewdisplay The only parameter is Surfaceholder */
Mcamera01.setpreviewdisplay (MSURFACEHOLDER01);
/* Run Preview immediately */
Mcamera01.startpreview ();
After the initialization is successful, it is possible to take pictures, and the camera function is still implemented by calling the camera class's function.
Mcamera01.takepicture
(Shuttercallback, Rawcallback, Jpegcallback);
Just implement Jpegcallback this callback function to decode and save the line, the first 2 parameters can be directly set to NULL, but the system will generally automatically help you write these in
Private Picturecallback Jpegcallback = new Picturecallback ()
{
public void Onpicturetaken (byte[] _data, Camera _camera)
{
TODO Handle JPEG image data
/* Onpicturetaken The first parameter passed in is a byte of the photo */
Bitmap BM = Bitmapfactory.decodebytearray
(_data, 0, _data.length);
/* Create a new file */
Picname = "sdcard/1234566.jpg";//Where to save, the path you set
File Mycapturefile = new file (picname);
Try
{
Bufferedoutputstream BOS = new Bufferedoutputstream
(New FileOutputStream (Mycapturefile));
/* Use the Compress-shift method */
Bm.compress (Bitmap.CompressFormat.JPEG, N, BOS);
/* Call the Flush () method to update Bufferstream */
Bos.flush ();
/* End OutputStream */
Bos.close ();
/* The picture file that will be photographed and stored is displayed */
Mimageview01.setimagebitmap (BM);
/* Display the picture file, reset the camera immediately, and close the preview */
Resetcamera ();
}
catch (Exception e)
{
LOG.E (TAG, E.getmessage ());
}
}
};
You're done. To reset the camera, then you can continue taking pictures
/* Camera Reset */
private void Resetcamera ()
{
if (MCAMERA01! = null && bifpreview)
{
Mcamera01.stoppreview ();
/* Expand learning to release the camera object */
Mcamera01.release ();
MCAMERA01 = null;
Bifpreview = false;
}
}
Comparison of 2 modes of photography
1. Call the system comes with the camera, the size of the photo format only a few choices, the photo shoot out relatively large, and the implementation of their own program can adjust the size of the photo to any size, picture capacity can be adjusted
2. Call the system simple, and the appearance is generally more than its own set of lot better
3. The operation of the call system is simple, convenient, error-prone, self-programming words need attention, easy to cause system error unexpected termination
http://blog.csdn.net/napolun007/article/details/6103307
2 ways to take pictures with Android