Android simple camera program instance code

Source: Internet
Author: User

Copy codeThe Code is as follows: class surface extends SurfaceView implements SurfaceHolder. Callback {

SurfaceHolder sfholder;

Camera camera;

Bitmap bitmap;

Public surface (Context context ){
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;

}
// Take a photo
Public void takePicture ()
{
If (camera! = Null)
{
Camera. takePicture (null, null, callback );
}
}

// Callback function for taking a photo and saving it

Private PictureCallback callback = 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, 80, 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 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:

Copy codeThe Code is 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 a camera and an SD card, you must add the following permissions:

Copy codeThe Code is as follows: android: name = "Android. permission. WRITE_EXTERNAL_STORAGE">

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:

Copy codeThe Code is as follows: if (Environment. getExternalStorageState (). equals (android. OS. Environment. MEDIA_MOUNTED )){
File sdcardPath = Environment. getExternalStorageDirectory ();
SetTitle (sdcardPath. getName (); // set the SD card path name as the title, just to make everyone better understand
} Else {
Toast. makeText (RecordTest. this, "No SD card", 1000). show ();
}
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.