Copy Code code as follows:
Class Surface extends Surfaceview implements Surfaceholder.callback {
Surfaceholder Sfholder;
Camera Camera;
Bitmap Bitmap;
Public Surface {
Super (context);
TODO auto-generated Constructor stub
Sfholder = Getholder ();
Sfholder.addcallback (this);
Sfholder.settype (surfaceholder.surface_type_push_buffers);
}
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 (320, 480);
Parameters.setpicturesize (320, 480);
Camera.setparameters (parameters);
Camera.startpreview ();
}
public void surfacecreated (Surfaceholder holder) {
TODO auto-generated Method Stub
try {
Camera = Camera.open ();
Camera.setpreviewdisplay (Sfholder);
catch (Exception e) {
Camera.release ();
camera = null;
}
}
public void surfacedestroyed (Surfaceholder holder) {
TODO auto-generated Method Stub
Camera.stoppreview ();
camera = null;
}
Photo
public void Takepicture ()
{
if (camera!= null)
{
Camera.takepicture (null, NULL, jpegcallback);
}
}
A callback function that is photographed and saved
Private Picturecallback Jpegcallback = new Picturecallback () {
public void Onpicturetaken (byte[] data, Camera Camera) {
TODO auto-generated Method Stub
Try
{
Bitmap = Bitmapfactory.decodebytearray (data, 0, data.length);
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);
}
catch (Exception e)
{
E.printstacktrace ();
}
}
}
}
This is a photo-taking class, the use of surfaceview to achieve, if you want to display in a part, and use the button to operate, you can modify this class, and this is a photo to stay in the photo page, if you need to take a picture after the photo page, you can modify and add parameters.
This program also demonstrates how to save a photo to an SD card after taking a picture.
Actual use of the operation, the use of the key to take pictures, in fact, you can use the button to take photos, as long as the class to make a simple change. The call is as follows:
Copy Code code as follows:
@Override
public boolean onKeyDown (int keycode, keyevent event) {
TODO auto-generated Method Stub
if (keycode = = Keyevent.keycode_dpad_center)
Sf.takepicture ();
Return Super.onkeydown (KeyCode, event);
}
To use the camera and operate the SD card, you need to add the following permissions:
Copy Code code as follows:
Android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" >
The first one is the ability to use the extended storage device
The second rule is that you can use camera devices
If you do not have these two permissions, will be an error, specific errors can be viewed in the DDMS. Welcome to the Exchange!
Hope to help you!
This platform uses a Android2.2 V8 system, and if you use 1.5 or something else, permissions may be different!
Of course, the better way is to first check the existence of the SD card, this program does not detect, because I just practice the use of the camera.
The detection code is as follows:
Copy Code code as follows:
if (Environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED)) {
File Sdcardpath = Environment.getexternalstoragedirectory ();
Settitle (Sdcardpath.getname ()); Set the SD card path name to the title, just to make it more clear
} else {
Toast.maketext (Recordtest.this, "No SD card", 1000). Show ();
}