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

1.布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn_camera"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black"
        android:textColor="@android:color/white"
        android:text="Camera" />

    <Button
         android:id="@+id/change"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="@android:color/black"
         android:textColor="@android:color/white"
         android:text="Change"/>

    <SurfaceView
        android:id="@+id/mySurfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

 

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<cameraCount; cameraIndex++){
            Camera.getCameraInfo(cameraIndex, info);
            if(info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
                frontIndex = cameraIndex;
            }else if(info.facing == Camera.CameraInfo.CAMERA_FACING_BACK){
                backIndex = cameraIndex;
            }
        }

        currentCameraType = type;
        if(type == FRONT && frontIndex != -1){
            return Camera.open(frontIndex);
        }else if(type == BACK && backIndex != -1){
            return Camera.open(backIndex);
        }
        return null;
    }

    private class MySurfaceViewCallback implements SurfaceHolder.Callback {
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                                   int height) {
            // TODO Auto-generated method stub
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            MainActivity.this.cam=Camera.open(); //取第一个摄像头
            WindowManager manager=(WindowManager)MainActivity.this.getSystemService(Context.WINDOW_SERVICE);
            Display display = manager.getDefaultDisplay();
            Camera.Parameters param=cam.getParameters();
            param.setPreviewSize(display.getWidth(), display.getHeight());
            param.setPreviewFrameRate(5);//一秒5帧
            param.setPictureFormat(PixelFormat.JPEG);
            param.set("jpeg-quality", 80);
            try{
                cam.setParameters(param);
            }catch(Exception e){
                e.printStackTrace();
            }
            try {
                cam.setPreviewDisplay(holder);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            cam.startPreview();//预览
            previewRunning=true;
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            if(cam!=null){
                if(previewRunning){
                    cam.stopPreview();
                    previewRunning=false;
                }
                cam.release();//释放摄像头
            }
        }
    }

    private class AutoFocusCallbackImpl implements Camera.AutoFocusCallback {

        @Override
        public void onAutoFocus(boolean success, Camera camera) {
            // 对焦操作
            if(success){
                cam.takePicture(sc, pc, jpegcall);
            }
        }

    }

    private Camera.PictureCallback jpegcall=new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // 保存图片操作
            Bitmap bmp= BitmapFactory.decodeByteArray(data, 0, data.length);
            String fileName= Environment.getExternalStorageDirectory().toString()
                    +File.separator
                    +"AppTest"
                    +File.separator
                    +"PicTest_"+System.currentTimeMillis()+".jpg";
            File file=new File(fileName);
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdir();//创建文件夹
            }
            try {
                BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(file));
                bmp.compress(Bitmap.CompressFormat.JPEG, 80, bos);//向缓冲区压缩图片
                bos.flush();
                bos.close();
                Toast.makeText(MainActivity.this, "拍照成功,照片保存在"+fileName+"文件之中!", Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
                Toast.makeText(MainActivity.this, "拍照失败!"+e.toString(), Toast.LENGTH_LONG).show();
            }
            cam.stopPreview();
            cam.startPreview();
        }
    };

    private android.hardware.Camera.ShutterCallback sc = new android.hardware.Camera.ShutterCallback() {

        @Override
        public void onShutter() {
            // 按下快门之后进行的操作
        }
    };
    private android.hardware.Camera.PictureCallback pc = new android.hardware.Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, android.hardware.Camera camera) {

        }
    };

}

 

3.AndroidManifest.xml文件

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
    tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值