Two Methods for photo taking in Android

Source: Internet
Author: User

Two photography methods have been implemented for the Android system for your reference:

1. Call the System camera to take a photo.

First, find the androidmanifest. xml file to add user permissions.

<Uses-Permission Android: Name = "android. Permission. Camera"> </uses-Permission>

<Uses-feature Android: Name = "android. Hardware. Camera"/>
<Uses-feature Android: Name = "android. Hardware. Camera. autofocus"/>

Secondly, adding two controls (buttons and imageview) to the main java file is used to trigger button events and display images. They are purely personal hobbies.

Final int take_picture = 1; // to identify the camera opened by your program in the return Method

Key: startactivityforresult (new intent ("android. Media. Action. image_capture"), take_picture );

Is to open the system comes with the camera, the following is to process the photo data, 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); // Private imageview IMG;
File mycapturefile = new file ("sdcard/123456.jpg ");
Try {
Bufferedoutputstream Bos = new bufferedoutputstream (New fileoutputstream (mycapturefile ));
/* Use the compressed file transfer method */
BM. Compress (bitmap. compressformat. JPEG, 80, 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 ();
}
}
}
}

In this way, you can call the camera that comes with the system. This is a simple operation.

2. Write a program to save the photo

These definitions must be implemented in the photo pattern file lay. xml.

<? 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>

Surfaceview is used for preview,

Initialize a series of values in the oncreat function:

Requestwindowfeature (window. feature_no_title );
Setcontentview (R. layout. Lay );

/* Retrieve 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 to obtain the surfaceholder object */
Msurfaceholder01 = msurfaceview01.getholder ();

/* The activity must implement surfaceholder. Callback */
Msurfaceholder01.addcallback (takephoto. This );

/*
* Use surface_type_push_buffers (3)
* Display type as surfaceholder
**/
Msurfaceholder01.settype
(Surfaceholder. surface_type_push_buffers );

 

First, initialize the camera function and parameter settings:

Private camera mcamera01;

Mcamera01 = camera. open ();

/* Create a camera. Parameters object */
Camera. parameters = mcamera01.getparameters ();

/* Set the photo format to JPEG */
Parameters. setpictureformat (pixelformat. JPEG );
Log. I (TAG, "pic is jpeg ");

/* Specify the screen size of the preview */
Parameters. setpreviewsize (320,240 );
Log. I (TAG, "pic pingmu fenbianlv ");

/* Set the image resolution size */
Parameters. setpicturesize (1024,768 );
Log. I (TAG, "pic tupian fenbianlv ");

/* Set camera. parameters to camera */
Mcamera01.setparameters (parameters );

/* The unique setpreviewdisplay parameter is surfaceholder */
Mcamera01.setpreviewdisplay (msurfaceholder01 );

/* Run Preview now */
Mcamera01.startpreview ();

After initialization, you can take a photo. The camera function is still implemented by calling the functions of the camera class.

Mcamera01.takepicture
(Shuttercallback, rawcallback, callback );

You only need to implement the callback function of callback for decoding and saving. The first two parameters can be directly set to null. However, the system will automatically write these parameters.

Private picturecallback callback = new picturecallback ()
{
Public void onpicturetaken (byte [] _ data, camera _ camera)
{
// Todo handle JPEG Image Data

/* The first parameter passed in by onpicturetaken is the byte of the photo */
Bitmap Bm = bitmapfactory. decodebytearray
(_ Data, 0, _ data. Length );

/* Create a new file */
Picname = "sdcard/1234566.jpg"; // you can specify the path where to store the data.
File mycapturefile = new file (picname );
Try
{
Bufferedoutputstream Bos = new bufferedoutputstream
(New fileoutputstream (mycapturefile ));

/* Use the compressed file transfer method */
BM. Compress (bitmap. compressformat. JPEG, 80, Bos );

/* Call the flush () method to update bufferstream */
Bos. Flush ();

/* End outputstream */
Bos. Close ();

/* Display the stored graph file */
// Mimageview01.setimagebitmap (BM );

/* After the image file is displayed, reset the camera immediately and disable preview */
Resetcamera ();

}
Catch (exception E)
{
Log. E (TAG, E. getmessage ());
}
}
};

 

After taking the photo, you need to reset the camera and continue taking the photo.

/* Reset the camera */
Private void resetcamera ()
{
If (mcamera01! = NULL & bifpreview)
{
Mcamera01.stoppreview ();
/* Extended learning to release the camera object */
Mcamera01.release ();
Mcamera01 = NULL;
Bifpreview = false;
}
}

 

Comparison of Two Photo Taking methods

1. Call the camera that comes with the system. There are only a few options for the photo format. The photo size is relatively large, and you can adjust the size of the photo to any size by using your own program. The image size can be adjusted.

2. The calling system is simple and looks better than what you set.

3. The call system is simple, convenient, and error-prone. If you program yourself, you need to pay attention to it, which may lead to unexpected termination of system errors.

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.