Android拍照以及前后摄像头切换

1.布局文件



    

 

2.代码

package com.example.lihui.androidproject;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;

import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends Activity{

    private Button btnCamera=null;
    private Button chgCamera=null;
    private SurfaceView mySurfaceView =null;
    private Camera cam=null;
    private SurfaceHolder holder=null;
    private boolean previewRunning=false;
    private static final int FRONT = 1;//前置摄像头标记
    private static final int BACK = 2;//后置摄像头标记
    private int currentCameraType = -1;//当前打开的摄像头标记

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= 23) {
            int REQUEST_CODE_CONTACT = 101;
            String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA};
            for (String str : permissions) {
                if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
                    this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
                    return;
                }
            }
        }

        btnCamera=findViewById(R.id.btn_camera);
        chgCamera=findViewById(R.id.change);
        mySurfaceView=findViewById(R.id.mySurfaceView);
        holder=this.mySurfaceView.getHolder();
        holder.addCallback(new MySurfaceViewCallback());
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        holder.setFixedSize(800, 480);
        currentCameraType = BACK;

        btnCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(cam!=null){
                    cam.autoFocus(new AutoFocusCallbackImpl());
                }
            }
        });

        chgCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    changeCamera();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void changeCamera() throws IOException{
        if(previewRunning){
            cam.setPreviewCallback(null);
            cam.stopPreview();
            cam.lock();
            cam.release();
            cam = null;
        }
        if(currentCameraType == FRONT){
            cam = openCamera(BACK);
        }else if(currentCameraType == BACK){
            cam = openCamera(FRONT);
        }
        cam.setPreviewDisplay(holder);
        cam.startPreview();
    }

    @SuppressLint("NewApi")
    private Camera openCamera(int type){
        int frontIndex =-1;
        int backIndex = -1;
        int cameraCount = Camera.getNumberOfCameras();
        Camera.CameraInfo info = new Camera.CameraInfo();
        for(int cameraIndex = 0; cameraIndex 
  

 

3.AndroidManifest.xml文件

你可能感兴趣的:(Android)