Android Application Development and Improvement (5)-----Camera use

Source: Internet
Author: User

Link Address: http://www.cnblogs.com/lknlfy/archive/2012/03/06/2382679.html

I. Overview

Camera is a very important device for mobile phones, which can be said on every mobile phone now. Recall that at that time in the Linux camera programming really want to Nieyibahan. Someone would say that it's possible to use a webcam with a OpenCV under Linux, but I didn't do it, and I did it in the most primitive Way (V4L2). Before the Android on the camera this part of the source code, no doubt, Android on the bottom of the camera implementation is also V4L2.

Second, the realization

Create a new project Mycamera, modify the/res/layout/main.xml file, use the Relativelayout layout, and add a surfaceview and a button, complete with Main.xml as follows:

1 <?xml version= "1.0" encoding= "Utf-8"?>
2 <relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
3 android:layout_width= "Fill_parent"
4 android:layout_height= "Fill_parent"
5 android:orientation= "vertical"
6 >
7
8
9 android:id= "@+id/surfaceview"
Ten android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_alignparenttop= "true"
android:layout_above= "@+id/button"
/>
15
16
android:id= "@+id/button"
android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_alignparentbottom= "true"
android:text= "Photo"
/>
23
</RelativeLayout>

Next, modify the Mycameraactivity.java file, implement the Surfaceholder.callback interface, used to initialize, release the camera and Camera.picturecallback interface, to save the image data into the file. The complete contents are as follows:

1 package Com.nan.camera;
2
3 Import Java.io.File;
4 Import java.io.FileNotFoundException;
5 Import Java.io.FileOutputStream;
6 Import java.io.IOException;
7
8 Import android.app.Activity;
9 Import Android.graphics.Bitmap;
Ten import android.graphics.BitmapFactory;
Import Android.graphics.PixelFormat;
Import Android.hardware.Camera;
Import Android.os.Bundle;
Import android.os.Environment;
Android.view.SurfaceHolder import;
Android.view.SurfaceView import;
+ Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.Toast;
20
$ public class Mycameraactivity extends Activity
22 {
Surfaceview Msurfaceview = null;
Private Surfaceholder msurfaceholder = null;
Private Button Takepicturebutton = null;
+ Private Camera Mcamera;
27//records are saved in the first few pictures
-private int whichpicture = 0;
29
/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate)
33 {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
36
PNS Takepicturebutton = (Button) This.findviewbyid (R.id.button);
Msurfaceview = (Surfaceview) This.findviewbyid (R.id.surfaceview);
Msurfaceholder = Msurfaceview.getholder ();
Msurfaceholder.addcallback (New Surfaceholdercallback ());
41//Set Buffer type
Msurfaceholder.settype (surfaceholder.surface_type_push_buffers);
43//Camera button monitoring
Takepicturebutton.setonclicklistener (New View.onclicklistener ()
45 {
46
@Override
-public void OnClick (View v)
49 {
/TODO auto-generated Method stub
51//Photo
Mcamera.takepicture (null, NULL, picturecallback);
53}
54});
55
56}
57
Implements Surfaceholder.callback, private class Surfaceholdercallback
59 {
60
@Override
surfacechanged public void (surfaceholder holder, int format, int width,
int height) {
+//TODO auto-generated method stub
65
66}
67
@Override
public void surfacecreated (Surfaceholder holder)
70 {
/TODO auto-generated Method stub
72//Turn on the camera
Mcamera = Camera.open ();
try {
Mcamera.setpreviewdisplay (Msurfaceholder);
* catch (IOException e) {
/TODO auto-generated Catch block
E.printstacktrace ();
79}
80//Get camera parameters
Bayi camera.parameters mparameters = Mcamera.getparameters ();
82//Set Picture format
Mparameters.setpictureformat (Pixelformat.jpeg);
Mcamera.setparameters (mparameters);
85//Start preview
Mcamera.startpreview ();
87}
88
@Override
public void surfacedestroyed (Surfaceholder holder)
91 {
//TODO auto-generated method stub
if (mcamera!=null)
94 {
95//Stop Preview
Mcamera.stoppreview ();
97//Release camera
98 mcamera.release ();
Mcamera = null;
100}
101}
102
103}
104
105//Photo callback
106 private Camera.picturecallback Picturecallback = new Camera.picturecallback ()
107 {
108
109 @Override
public void Onpicturetaken (byte[] data, camera camera)
111 {
/TODO auto-generated Method stub
113//Stop Preview
Mcamera.stoppreview ();
Bitmap Mbitmap;
Mbitmap = Bitmapfactory.decodebytearray (data, 0, data.length);
117//file path and filename
118 File PictureFile = new file (Environment.getexternalstoragedirectory (), "Camera" +integer.tostring (whichpictu RE) + ". jpg");
119
Try
121 {
122 FileOutputStream Mfileoutputstream = new FileOutputStream (picturefile);
123//Press the image data into the file
124 mbitmap.compress (Bitmap.CompressFormat.JPEG, Mfileoutputstream);
try {
126//Turn off the output stream
127 mfileoutputstream.close ();
* catch (IOException e) {
129//TODO auto-generated catch block
E.printstacktrace ();
131}
132}
133 catch (FileNotFoundException e)
134 {
135//TODO auto-generated catch block
136 E.printstacktrace ();
137}
138 Displaytoast ("saved successfully! ");
139 whichpicture++;
140//Start preview
141 Mcamera.startpreview ();
142}
143
144};
145
146//Show Toast function
147 private void Displaytoast (String s)
148 {
149 Toast.maketext (this, S, Toast.length_short). Show ();
150}
151
152}

Run the program on the real machine:

Click on the "Take Photo" button:

The image will be saved to the root directory of the SD card.

Android Application Development and Improvement (5)-----Camera use

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.