This article describes the Android program to achieve mobile phone camera method. Share to everyone for your reference, specific as follows:
Today I got almost a day of cell phone photos, and later, to think, and now do not know what they know is right, first of all, if the use of this way to take pictures, the program in the simulator, just start will be a problem, do not know what the reason, guess probably because it is the cause of the simulator, there is no mobile phone to test, This is not explained by the following code:
Intent Intent = new Intent (mediastore.action_image_capture);
Startactivityforresult (Intent, 0);
The following is to get the photo code, because I need to return directly to the current page preview, and need to save the address, but here I simply write to receive data, how to save the photo, here does not tell, receive photos data code as follows:
Bundle extras = Data.getextras ();
Bitmap B = (Bitmap) extras.get ("Data");
But at the time of receiving, need to first judge is not empty, otherwise prone to error, received, we can carry out data storage operations, but do not know why, this way in the simulator can not be implemented, it may be necessary hardware support it, because the program needs, so many times, A number of different SDK simulator test, has not been successful.
The call was later changed in this way:
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.PixelFormat;
Import Android.hardware.Camera;
Import Android.hardware.Camera.PictureCallback;
Import Android.os.Bundle;
Import Android.view.SurfaceHolder;
Import Android.view.SurfaceView;
Import Android.view.View;
Import Android.widget.Button;
public class Camera1 extends activity implements Surfaceholder.callback {Surfaceview sfview;
Surfaceholder Sfholder;
Camera Camera;
Button btn1, btn2;
Byte[] Bitmpdata; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
BTN1 = (Button) Findviewbyid (R.ID.BTN1);
BTN2 = (Button) Findviewbyid (R.ID.BTN2);
Sfview = (Surfaceview) Findviewbyid (R.id.surface1); Sfholder = sfview.geTholder (); Sfholder.settype (surfaceholder.surface_type_push_buffers);
Must have a type to display, otherwise it will not display sfholder.addcallback (this); Btn1.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {//TODO auto-generated Me
Thod stub camera.takepicture (null, NULL, piccallback);
}
}); Btn2.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {//TODO auto-generated Me
Thod stub savepic ();
}
}); public void surfacechanged (surfaceholder holder, int format, int width, int height) {//TODO auto-generated
Method stub Camera.parameters Parameters = Camera.getparameters ();
Parameters.setpictureformat (Pixelformat.jpeg);
Parameters.setpreviewsize (width, height);
Parameters.setpicturesize (320, 480);
Camera.setparameters (parameters);
Camera.startpreview ();
} public void surfacecreated (Surfaceholder holder) {//TODO auto-generated stub try {camera = Camera.open ();
Camera.setpreviewdisplay (Sfholder);
Camera.autofocus (New Camera.autofocuscallback () {public void Onautofocus (Boolean success, camera camera) {
TODO auto-generated Method Stub if (success) camera.takepicture (null, NULL, piccallback);
}
});
catch (Exception e) {camera.release ();
camera = null; } public void surfacedestroyed (Surfaceholder holder) {//TODO auto-generated method stub Camera.stoppreview
();
camera = null; Private Picturecallback Piccallback = new Picturecallback () {public void Onpicturetaken (byte[] data, Camera Camer
A) {//TODO auto-generated method Stub bitmpdata = data;
}
}; private void Savepic () {try {Bitmap Bitmap = Bitmapfactory.decodebytearray (bitmpdata, 0, BITMPDATA.L
Ength);
File File = new file ("/sdcard/camera1.jpg"); Bufferedoutputstream BOS = new BuffereDoutputstream (new FileOutputStream (file));
Bitmap.compress (Bitmap.CompressFormat.JPEG, BOS);
Bos.flush ();
Bos.close ();
Canvas canvas= Sfholder.lockcanvas ();
Canvas.drawbitmap (bitmap, 0,0, null);
Sfholder.unlockcanvasandpost (canvas);
Camera.stoppreview ();
Camera.startpreview ();
catch (Exception e) {e.printstacktrace ();
}
}
}
This way, you can get the code, you can save the data, but for how to focus automatically, do not know how to control, for how to zoom in or zoom out of the preview of the picture is not clear, check the API also did not find, but this way, in the camera mode can be run on the simulator, Others also require the support of the real machine, and:
Sfholder.settype (surfaceholder.surface_type_push_buffers);
Must have a type to display, otherwise it will not display
You need to set this type, otherwise it cannot be opened!
For more information on Android-related content readers can view the site: "Android Multimedia how-to Summary (audio, video, recording, etc.)", "Android Development introduction and Advanced Course", "Android view summary of the tips", " The activity Operation skill summary of Android programming, "Android operation SQLite database Skill Summary", "Android operation JSON format Data technique summary", "Android database Operation skill Summary", "Android File Operation skill Summary "," Android programming development of SD card operation method Summary "," Android Resource Operation skills Summary "and" Android Control usage Summary "
I hope this article will help you with the Android program.