camra2

package map.zhongke.myapplication;


import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.media.Image;
import android.media.ImageReader;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.View;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;

import static android.graphics.ImageFormat.NV21;

public class MainActivity extends AppCompatActivity  {
    private String tag = "MainActivity:";

    private void log(Object sd) {
        String vad = sd.toString();
        Log.e(tag, vad);
    }

    CameraManager cameraManager;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        log("oncreate");
        initCamera2();
   openCamera();
    }
 private ImageReader mImageReader;
    Handler childHandler,mainHandler;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initCamera2() {
        HandlerThread handlerThread = new HandlerThread("Camera2");
        handlerThread.start();
        childHandler = new Handler(handlerThread.getLooper());
        mainHandler = new Handler(getMainLooper());

        mImageReader = ImageReader.newInstance(1080, 1920, ImageFormat.JPEG,100);
        mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() { //可以在这里处理拍照得到的临时照片 例如,写入本地
            @Override
            public void onImageAvailable(ImageReader reader) {
            log("onImageAvailable");
                Image image = reader.acquireLatestImage();
                image.close();
            }
        }, mainHandler);

    }
    CameraCaptureSession.StateCallback  captureCallback=new CameraCaptureSession.StateCallback() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onConfigured(@NonNull CameraCaptureSession session) {
            log("onConfigured");
            CaptureRequest captureRequests=captureRequest.build();


            try {
                session.setRepeatingRequest(captureRequests,null,childHandler);
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }


        }

        @Override
        public void onConfigureFailed(@NonNull CameraCaptureSession session) {
            log("onConfigureFailed");
        }
    };
    CaptureRequest.Builder captureRequest;
    CameraDevice  cameraDevice;
    CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onOpened(@NonNull CameraDevice camera) {
            log("onOpened");
            cameraDevice=camera;

            try {
            captureRequest = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
                captureRequest.addTarget(mImageReader.getSurface());
                cameraDevice.createCaptureSession(Arrays.asList(mImageReader.getSurface()),captureCallback,childHandler);
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onDisconnected(@NonNull CameraDevice camera) {
            log("onDisconnected");
            camera.close();
            cameraDevice=null;
        }

        @Override
        public void onError(@NonNull CameraDevice camera, int error) {
            log("onError");
            camera.close();
            cameraDevice = null;
        }

        @Override
        public void onClosed(@NonNull CameraDevice camera) {
            super.onClosed(camera);
        }
    };

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void openCamera() {
        cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);

        try {
            String[] cameraIdList = cameraManager.getCameraIdList();
            for (int i = 0; i < cameraIdList.length; i++) {
                System.out.println(cameraIdList[i]);
            }

            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            cameraManager.openCamera(cameraIdList[0], stateCallback, mainHandler);

      } catch (CameraAccessException e) {
          e.printStackTrace();
      }


  }
  public void closeCamera(){

  }

    boolean  isrun =true;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void testBtn(View view){
        if (isrun){
            isrun=false;
            openCamera();
        }else {
            isrun=true;
            closeCamera();
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值