SoFunction
Updated on 2025-03-11

Detailed explanation of how to implement Android programming to take pictures on mobile phones

This article describes the method of Android programming to realize mobile phone photography. Share it for your reference, as follows:

I made a photo of my mobile phone for almost a day today. Later, I thought about it while working, and now I don’t know if the photo is right. First of all, if I use this method to take pictures, the program will have problems in the simulator as soon as it is started. I don’t know the reason. I guess it may be because it is an emulator. There is no mobile phone for testing. These cannot be explained. The code is as follows:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);

The following is the photo code to get the photo, because I need to preview the current page directly when I return and the address I need to save. However, here I just simply write about the receiving data and how to save the photo. I will not talk about it here. The receiving photo data code is as follows:

Bundle extras = ();
Bitmap b = (Bitmap) ("data");

However, when receiving, you need to determine whether it is empty first, otherwise errors are prone to errors. After receiving, we can perform data storage and other operations. However, for some reason, this method cannot be implemented in the simulator, and it may also require hardware support. Because the program needs it, multiple simulator tests of multiple different SDKs have been carried out, but it has never been successful.

Later, the call was changed to the following method:

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class Camera1 extends Activity implements  {
  SurfaceView sfView;
  SurfaceHolder sfHolder;
  Camera camera;
  Button btn1, btn2;
  byte[] bitmpdata;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
    btn1 = (Button) findViewById(.btn1);
    btn2 = (Button) findViewById(.btn2);
    sfView = (SurfaceView) findViewById(.surface1);
    sfHolder = ();
    (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // There must be a type to display, otherwise it will not be displayed    (this);
    (new () {
      public void onClick(View v) {
        // TODO Auto-generated method stub
        (null, null, picCallback);
      }
    });
    (new () {
      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
     parameters = ();
    ();
    (width, height);
    (320, 480);
    (parameters);
    ();
  }
  public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    try {
      camera = ();
      (sfHolder);
      (new () {
        public void onAutoFocus(boolean success, Camera camera) {
          // TODO Auto-generated method stub
          if (success)
            (null, null, picCallback);
        }
      });
    } catch (Exception e) {
      ();
      camera = null;
    }
  }
  public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    ();
    camera = null;
  }
  private PictureCallback picCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
      // TODO Auto-generated method stub
      bitmpdata = data;
    }
  };
  private void savePic() {
    try {
      Bitmap bitmap = (bitmpdata, 0,
          );
      File file = new File("/sdcard/");
      BufferedOutputStream bos = new BufferedOutputStream(
          new FileOutputStream(file));
      (, 80, bos);
      ();
      ();
      // Canvas canvas= ();
      // (bitmap, 0,0, null);
      // (canvas);
      ();
      ();
    } catch (Exception e) {
      ();
    }
  }
}

This method allows you to obtain code and save data, but you don’t know how to control how to automatically focus, and you don’t know how to enlarge or reduce the preview photos. After checking the API, you can not find it. However, this method can run on the simulator in taking photos. Some of the others also require real machine support, and:

(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// There must be a type to display, otherwise it will not be displayed

Need to be set to this type, otherwise it cannot be turned on!

For more information about Android related content, please check out the topic of this site:Android multimedia operation skills summary (audio, video, recording, etc.)》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Summary of Android's SQLite database skills》、《Summary of Android data skills for operating json format》、《Android database operation skills summary》、《Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android resource operation skills summary"and"Android control usage summary

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