After several errors, I modified a simple camera.ProgramFinished, photographyCodeAs follows:
Code
1 Class Surface Extends Surfaceview Implements Surfaceholder. Callback {
2
3 Surfaceholder sfholder;
4
5 Camera camera;
6
7 Bitmap bitmap;
8
9 Public Surface (context ){
10 Super (Context );
11 // Todo auto-generated constructor stub
12
13 Sfholder = Getholder ();
14 Sfholder. addcallback ( This );
15 Sfholder. settype (surfaceholder. surface_type_push_buffers );
16 }
17
18 Public Void Surfacechanged (surfaceholder holder, Int Format, Int Width,
19 Int Height ){
20 // Todo auto-generated method stub
21 Camera. parameters = Camera. getparameters ();
22 Parameters. setpictureformat (pixelformat. JPEG );
23 Parameters. setpreviewsize ( 320 , 480 );
24 Parameters. setpicturesize ( 320 , 480 );
25 Camera. setparameters (parameters );
26 Camera. startpreview ();
27 }
28
29 Public Void Surfacecreated (surfaceholder holder ){
30 // Todo auto-generated method stub
31
32 Try {
33 Camera = Camera. open ();
34 Camera. setpreviewdisplay (sfholder );
35 } Catch (Exception e ){
36 Camera. Release ();
37 Camera = Null ;
38 }
39 }
40
41 Public Void Surfacedestroyed (surfaceholder holder ){
42 // Todo auto-generated method stub
43 Camera. stoppreview ();
44 Camera = Null ;
45
46 }
47 // Take a photo
48 Public Void Takepicture ()
49 {
50 If (Camera ! = Null )
51 {
52 Camera. takepicture ( Null , Null , Callback );
53 }
54 }
55
56 // The callback function for taking the photo and saving it
57
58 Private Picturecallback callback = New Picturecallback (){
59
60 Public Void Onpicturetaken ( Byte [] Data, camera ){
61 // Todo auto-generated method stub
62 Try
63 {
64 Bitmap = Bitmapfactory. decodebytearray (data, 0 , Data. Length );
65 File = New File ( " /Sdcard/camera1.jpg " );
66 Bufferedoutputstream BOS = New Bufferedoutputstream ( New Fileoutputstream (File ));
67 Bitmap. Compress (bitmap. compressformat. JPEG, 80 , Bos );
68 Bos. Flush ();
69 Bos. Close ();
70 Canvas canvas = Sfholder. lockcanvas ();
71 Canvas. drawbitmap (bitmap, 0 , 0 , Null );
72 Sfholder. unlockcanvasandpost (canvas );
73 }
74 Catch (Exception E)
75 {
76 E. printstacktrace ();
77 }
78 }
79 }
80 }
This is a photo class, which is implemented by surfaceview. If you want to display a part of it and use buttons, you can modify this class, in addition, if you want to stay on the photo page after taking a photo, you can modify and add parameters on your own.
This program also demonstrates how to save the photo to the SD card after taking the photo.
In actual use, the button is used to take a photo. In fact, you can also use the button to take a photo, as long as you make a simple modification to this class. The call is as follows:
Code
@ 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 a camera and an SD card, you must add the following permissions:
<Uses-PermissionAndroid: Name= "Android. Permission. write_external_storage"> </Uses-Permission>
<Uses-PermissionAndroid: Name= "Android. Permission. Camera"> </Uses-Permission>
The first option is to extend the permissions of the storage device.
Article 2: camera devices can be used
If you do not have these two permissions, an error is returned. You can view the error in ddms. Welcome!
Hope to help you!
This platform uses the android 1.5 V8 system. If you use or another version, the permissions may be different!
Of course, the better way is to first check whether the SD card exists. This program does not detect it, because I am only using the exercise camera.
The check code is as follows:
Code
If (Environment. getexternalstoragestate (). Equals (Android. OS. environment. media_mounted )){
File sdcardpath = Environment. getexternalstoragedirectory ();
Settitle (sdcardpath. getname ()); // Set the path name of the SD card as the title to make everyone better understand.
} Else {
Toast. maketext (recordtest. This , " No SD card " , 1000 ). Show ();
}