English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Explanation of Method to Implement Phone Camera Function in Android Programming

This article describes the method of implementing mobile phone photography in Android programming. Shared with everyone for reference, as follows:

Today I spent almost a day dealing with mobile phone photography. Later, while dealing with it, I thought about it, and now I don't even know if what I know is correct. First, if this method is used for taking photos, the program will have problems when it starts up on the emulator, and I don't know what the reason is. I guess it might be because of the emulator. Currently, there is no phone for testing, and these things are unexplainable. The code is as follows:

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

Below is the code to obtain the photo, because I need to preview the current page directly when returning and also need to save the address, but here I just simply write the code for receiving data, and how to save the photo is not discussed here. The code for receiving photo data is as follows:

Bundle extras = data.getExtras();
Bitmap b = (Bitmap) extras.get("data");

However, when receiving, we need to first check if it is empty, otherwise it is easy to make mistakes. After receiving, we can proceed with operations such as data saving, but for some reason, this method cannot be implemented on the emulator, it may also require hardware support. Because the program requires it, I have tested it multiple times on multiple different SDK emulators, but it always fails.

Later, I changed to this method for calling:

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); // There must be 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 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 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 camera) {
      // TODO Auto-generated method stub
      bitmpdata = data;
    }
  };
  private void savePic() {
    try {
      Bitmap bitmap = BitmapFactory.decodeByteArray(bitmpdata, 0,
          bitmpdata.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);
      camera.stopPreview();
      camera.startPreview();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

This method can get the code and also save data, but I don't know how to control the automatic focus, nor how to zoom in or out the preview photo. I have checked the API and can't find it, but this method can run on the emulator in camera mode, and some others also need the support of a real device, and also:

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

This type must be set, otherwise it cannot be opened!

Readers who are interested in more about Android-related content can check out the special topics on this site: 'Summary of Android Multimedia Operation Skills (audio, video, recording, etc.)', 'Android Development Tutorial for Beginners and Advanced', 'Summary of Android View View Skills', 'Summary of Activity Operation Skills in Android Programming', 'Summary of SQLite Database Operation Skills in Android', 'Summary of JSON Data Format Operation Skills in Android', 'Summary of Database Operation Skills in Android', 'Summary of File Operation Skills in Android', 'Summary of SD Card Operation Methods in Android Programming Development', 'Summary of Android Resource Operation Skills', and 'Summary of Android Control Usage'

I hope this article will be helpful to everyone in Android program design.

Statement: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, does not undergo manual editing, and does not bear relevant legal responsibility. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report any infringement by sending an email to codebox.com (replace # with @ when sending an email), and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like