Detailed explanation of the method of calling camera function in Android development tutorial

  • 2021-07-26 08:57:34
  • OfStack

In this paper, the method of calling camera function by Android is described as an example. Share it for your reference, as follows:

We're going to call the camera's photo function, obviously

Step 1 must add the authority to call the camera hardware. After taking photos, we want to save the pictures in SD card, and must add the read-write authority of SD card. Therefore, step 1, we should add the following code in Android manifest file

Camera permissions:


<uses-permission android:name="android.permission.CAMERA"/>

SD card read and write permissions:


<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

The second step is to display the images captured by the camera on the mobile phone in real time.

We implemented it with the view component SurfaceView, so we added the following code in main. xml


<SurfaceView
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/surfaceview"
 />

Step 3: Set the display mode of the window

First get the current window


Window window = getWindow();// Get the window 

Then set no title


requestWindowFeature(Window.FEATURE_NO_TITLE);// No title 

Then set the full screen

window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// Set full screen 

Of course, when we take pictures, the screen must be highlighted, so add the following code

window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);// Set highlighting 

At this point, we have defined the display mode of the window, and then we can set the components displayed on the window. (The order is very important.)


setContentView(R.layout.main);

Step 4: Set the properties of the SurficeView display control

Find surficeView


surfaceView = (SurfaceView) findViewById(R.id.surfaceview);

Set its pixels to 800x600


surfaceView.getHolder().setFixedSize(800, 480);
// The following settings surfaceView Do not maintain your own buffer , Instead, wait for the rendering engine of the screen to push the content to the user 
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

Step 5 is to add a callback method (callBack) to surficeView


surfaceView.getHolder().addCallback(new SurfaceCallback());

The above callback class is defined by ourselves, and the code is as follows


private class SurfaceCallback implements SurfaceHolder.Callback{
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera = Camera.open();// Turn on the hardware camera. When guiding the package here, 1 It must be noted that android.hardware.Camera
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);// Get the window manager 
Display display = wm.getDefaultDisplay();// Get the current screen 
Camera.Parameters parameters = camera.getParameters();// Get the parameters of the camera 
parameters.setPreviewSize(display.getWidth(), display.getHeight());// Set the size of preview photos 
parameters.setPreviewFrameRate(3);// Set per second 3 Frame 
parameters.setPictureFormat(PixelFormat.JPEG);// Format photos 
parameters.setJpegQuality(85);// Set the quality of photos 
parameters.setPictureSize(display.getHeight(), display.getWidth());// Set the size of the photo, the default is and     Screen 1 Sample size 
camera.setParameters(parameters);
camera.setPreviewDisplay(surfaceView.getHolder());// Pass SurfaceView Display framing screen 
camera.startPreview();// Start previewing 
isPreview = true;// Sets whether the preview parameter is true 
} catch (IOException e) {
Log.e(TAG, e.toString());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(camera!=null){
if(isPreview){// If you are previewing 
camera.stopPreview();
camera.release();
}
}
}
}

Step 6, we must monitor the key events, such as taking pictures or focusing, the code is as follows


<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

0

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

1

Notice that there are two callback classes in the code, One is SurfaceCallback (), and the other is TakePictureCallback (), which may be difficult for beginners to understand. Generally speaking, the former is used to monitor surficeView, a display control that temporarily stores picture data. According to its display situation, different methods are called, including surfaceCreated (), surfaceChanged () and surfaceDestroyed (), so it is not difficult to understand why there are these three callback methods (note that the camera must be released in surfaceDestroyed () method. TakePictureCallback () is an interface designed to monitor whether to take pictures or not, and there is only one method in the period. camera passes the data obtained by taking pictures into the method, and we can process the data obtained by taking pictures one step further.

At this point, the simple photo function is introduced!


<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2

For more readers interested in Android related contents, please check the topics on this site: "Summary of Android File Operation Skills", "Summary of SD Card Operation Methods for Android Programming Development", "Introduction and Advanced Tutorial for Android Development", "Summary of Android Resource Operation Skills", "Summary of Android View View Skills" and "Summary of Android Control Usage"

I hope this article is helpful to everyone's Android programming.


Related articles: