SoFunction
Updated on 2025-04-06

Analysis of 2 methods of Android programming to implement photo shooting function

This article describes two methods of Android programming to implement the camera function. Share it for your reference, as follows:

The camera function of the Android system has implemented 2 methods for your reference:

1. Call the system camera to take pictures

First, find the file to add user permissions

<uses-permission android:name=""></uses-permission>
<uses-feature android:name="" />
<uses-feature android:name="" />

Secondly, add two controls (button and imageview) to the main Java file, which are used to trigger button events and display pictures, which is purely a personal hobby.

final int TAKE_PICTURE = 1;
//To indicate the camera that is opened in the return method to identify your program.

The key is here:

Copy the codeThe code is as follows:
startActivityForResult(new Intent(".IMAGE_CAPTURE"), TAKE_PICTURE);

Turn on the system's own camera. The following is the data obtained by processing the photo and save the data.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == TAKE_PICTURE) {
   if (resultCode == RESULT_OK) {
    Bitmap bm = (Bitmap) ().get("data");
    (bm);//I want the image to be displayed on the ImageView view, private ImageView img;    File myCaptureFile = new File("sdcard/");
    try {
      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
      /* Use compression shift method */
       (, 80, bos);
       /* Call the flush() method to update the BufferStream */
       ();
       /* End OutputStream */
       ();
     } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      ();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      ();
     }
   }
  }
}

This enables the call to the camera that comes with the system, which is very simple to operate.

2. Write your own program to save photos

These definitions should be made in the photo layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:andro
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
>
<LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:paddingLeft="130px"
  android:paddingRight="200px"
 >
 <SurfaceView
  android:
  android:visibility="visible"
  android:layout_width="320px"
  android:layout_height="240px">
 </SurfaceView>
 </LinearLayout>
 </LinearLayout>

The SurfaceView is used for previewing.
Initialize a series of values ​​in the Oncreat function:

requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView();
/* Get screen resolution pixels */
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// mImageView01 = (ImageView) findViewById(.myImageView1);
/* Use SurfaceView as the camera Preview */
mSurfaceView01 = (SurfaceView) findViewById(.mSurfaceView1);
/* Bind the SurfaceView and get the SurfaceHolder object */
mSurfaceHolder01 = ();
/* Activity must be implemented */
();
/*
 * SURFACE_TYPE_PUSH_BUFFERS(3)
 * as SurfaceHolder display type
 * */

(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

First, initialize the function and parameter settings of the camera:

private Camera mCamera01;
mCamera01 = ();
/* Create an object */
 parameters = ();
/* Set the photo format to JPEG */
();
(TAG, "pic is jpeg");
/* Specify the screen size of the preview */
(320, 240);
(TAG, "pic pingmu fenbianlv");
/* Set the image resolution size */
(1024, 768);
(TAG, "pic tupian fenbianlv");
/* Set to Camera */
(parameters);
/* The only parameter of setPreviewDisplay is SurfaceHolder */
(mSurfaceHolder01);
/* Run Preview now */
();

After the initialization is successful, you can take pictures. The photo function is still implemented by calling the camera class function.

Copy the codeThe code is as follows:
(shutterCallback, rawCallback, jpegCallback);

Just implement the callback function jpegCallback to decode and save. The first two parameters can be set to null directly, but the system will usually automatically write all of these for you.

private PictureCallback jpegCallback = new PictureCallback()
{
   public void onPictureTaken(byte[] _data, Camera _camera)
   {
    // TODO Handle JPEG image data
    /* The first parameter passed in onPictureTaken is the photo byte */
    Bitmap bm = 
          (_data, 0, _data.length);
    /* Create a new file */
       picname = "sdcard/";//Save where to save it, set the path yourself    File myCaptureFile = new File(picname);
    try
    {
     BufferedOutputStream bos = new BufferedOutputStream
     (new FileOutputStream(myCaptureFile));
     /* Use compression shift method */
     (, 80, bos);
     /* Call the flush() method to update the BufferStream */
     ();
     /* End OutputStream */
     ();
     /* The pictures and files that have been taken and stored are displayed */
     //(bm);
     /* After displaying the image file, reset the camera immediately and close the preview */
     resetCamera();
     }
    catch (Exception e)
    {
     (TAG, ());
    }
   }
};

After taking pictures, you need to reset the camera and then continue taking pictures

/* Camera reset */
private void resetCamera()
{
   if (mCamera01 != null &amp;&amp; bIfPreview)
   {
    ();
    /* Extended learning, release Camera objects */
    ();
    mCamera01 = null;
    bIfPreview = false;
   }
}

Comparison of 2 ways of taking pictures

①. Call the camera that comes with the system. There are only a few choices for the photo format and size. The photos are taken relatively large. If you implement them yourself, you can adjust the size of the photo to any size, and the capacity of the picture can be adjusted.

②. Calling the system is simple, and the appearance is generally better than the ones you set

③. The operation of calling the system is simple and convenient, and it is not prone to errors. You need to pay attention to it if you program it yourself, which can easily cause system errors and unexpected termination.

For more information about Android related content, please check out the topic of this site:Summary of Android photography and picture processing skills》、《Summary of Android graphics and image processing skills》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.