1. Customize the camera preview and take a photo
package com.android.myapplication; import android.content.ContentValues; import android.content.res.Configuration; import android.hardware.Camera; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.widget.Toast; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import java.util.List; public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback ,Camera.PictureCallback { //The size of SurfaceView in xml private static final int LARGEST_WIDTH = 200; private static final int LARGEST_HEIGHT = 200; private android.view.SurfaceView svCamera; private Camera camera; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_camera); initView(); initData (); } private void initData() { SurfaceHolder holder = svCamera.getHolder(); holder.addCallback(this); } private void initView() { svCamera = (SurfaceView) findViewById(R.id.sv_camera); } @Override public void surfaceCreated(SurfaceHolder holder) { int numberOfCameras = Camera.getNumberOfCameras(); Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); //Test the code to see how many cameras are available if(numberOfCameras>1){ for(int i=0;i<numberOfCameras;i++){ Camera.getCameraInfo(i,cameraInfo); if(cameraInfo.facing== Camera.CameraInfo.CAMERA_FACING_BACK){ Log.e("==","后置"); }else if(cameraInfo.facing== Camera.CameraInfo.CAMERA_FACING_FRONT){ Log.e("==","前置"); }else { Log.e("==","其他"); } } } try { //After the above query, it is found that the judgment before and after different devices is not accurate. For example, the id of the front camera of our device is 0 instead of 1. camera = Camera.open(0); //The default preview of the system is landscape mode, and the vertical screen preview needs a rotation angle Camera.Parameters parameters = camera.getParameters(); if(getResources().getConfiguration().orientation!= Configuration.ORIENTATION_LANDSCAPE){//竖屏 parameters.set("orientation","portrait"); camera.setDisplayOrientation(90); parameters.setRotation(90); }else{//横屏 parameters.set("orientation","landscape"); camera.setDisplayOrientation(0); parameters.setRotation(0); } //add special effects List<String> supportedColorEffects = parameters.getSupportedColorEffects(); for (String currentEffect : supportedColorEffects) {//Query whether related effects are supported if (Camera.Parameters.EFFECT_MONO.equals(currentEffect)){ parameters.setColorEffect(currentEffect); break; } } //Change the camera preview size //First query the list of all sizes supported by the device int bestWidth = 0; int bestHeight = 0; List<Camera.Size> supportedPictureSizes = parameters.getSupportedPictureSizes(); if(supportedPictureSizes.size()>1){ for (Camera.Size aSize : supportedPictureSizes) { //Whether the set size meets the size conditions supported by the device: the supported size is greater than 0 and less than or equal to our custom size, which is reasonable if(aSize.width>bestWidth && aSize.width<=LARGEST_WIDTH && aSize.height>bestHeight && aSize.height<=LARGEST_HEIGHT){ bestWidth=aSize.width; bestHeight=aSize.height; break; } } if(bestWidth!=0 && bestHeight!=0){ parameters.setPreviewSize(bestWidth,bestHeight); } } camera.setParameters(parameters); camera.setPreviewDisplay(holder); camera.startPreview(); } catch (IOException e) { e.printStackTrace (); camera.release(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release(); } public void take_photo(View view) { if(camera!=null){ camera.takePicture(null,null,this); } } @Override public void onPictureTaken(byte[] data, Camera camera) { Uri imgFileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues()); try { OutputStream imgFileOS = getContentResolver().openOutputStream(imgFileUri); imgFileOS.write(data); imgFileOS.flush(); imgFileOS.close(); Toast.makeText(this, "The picture has been saved!", Toast.LENGTH_SHORT).show(); camera.startPreview(); } catch (FileNotFoundException e) { e.printStackTrace (); } catch (IOException e) { e.printStackTrace (); } } }xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.android.myapplication.CameraActivity"> <SurfaceView android:id="@+id/sv_camera" android:layout_width="200dp" android:layout_height="200dp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_below="@+id/sv_camera" android:layout_marginStart="52dp" android:layout_marginTop="16dp" android:onClick="take_photo" android:text="Photo" /> </RelativeLayout>