Today, I took a photo on my cell phone for almost one day. Later, I thought about it, and now I don't know what I know, right? First, if I use this method to take a photo,ProgramIn the simulator, problems may occur when it is started. I don't know what the cause is. I guess it may be because of the simulator. At present, there is no mobile phone for testing, which cannot be explained,CodeAs follows:
Intent intent= NewIntent (mediastore. action_image_capture );
Startactivityforresult (intent,0);
The following figure shows how to get the photo code, because I need to preview the code to the current page and save the address when returning the code. But here I just want to write a simple description of the received data and how to save the photo, the code for receiving photo data is as follows:
Bundle extras=Data. getextras ();
Bitmap B=(Bitmap) extras. Get ("Data");
However, when receiving the message, you must first determine whether it is null. Otherwise, errors may occur. After receiving the message, we can save the data and perform other operations, but we do not know why, this method cannot be implemented on the simulator, or it may require hardware support. Because the program needs it, it has been tested multiple times and multiple simulation machines with different sdks have never been successful.
Later, call the following method:
Code
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 when the activity is first created. */
@ 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 be displayed
Sfholder. addcallback ( This );
Btn1.setonclicklistener ( New View. onclicklistener (){
Public Void Onclick (view v ){
// Todo auto-generated method stub
Camera. takepicture ( Null , Null , Piccallback );
}
});
Btn2.setonclicklistener ( New View. onclicklistener (){
Public Void Onclick (view v ){
// Todo auto-generated method stub
Savepic ();
}
});
}
Public Void Surfacechanged (surfaceholder holder, Int Format, Int Width,
Int Height ){
// Todo auto-generated method stub
Camera. 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 method stub
Try {
Camera = Camera. open ();
Camera. setpreviewdisplay (sfholder );
Camera. autofocus ( New Camera. autofocuscallback (){
Public Void Onautofocus ( Boolean Success, 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 ){
// Todo auto-generated method stub
Bitmpdata = Data;
}
};
Private Void Savepic (){
Try {
Bitmap bitmap = Bitmapfactory. decodebytearray (bitmpdata, 0 ,
Bitmpdata. Length );
File = New File ( " /Sdcard/camera1.jpg " );
Bufferedoutputstream BOS = New Bufferedoutputstream (
New Fileoutputstream (File ));
Bitmap. Compress (bitmap. compressformat. JPEG, 80 , 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 ();
}
}
}
In this way, you can get the code or save the data, but you do not know how to automatically focus, how to control it, and how to zoom in or zoom out the preview photos, I did not check the API, but in this way, the camera can run on the simulator, and some others also need the support of the real machine, and:
Sfholder. settype (surfaceholder. surface_type_push_buffers );//Must have a type to display, otherwise it will not be displayed
You need to set this type, otherwise it cannot be enabled!
If you have any experience in this area, please contact me!