package com.intsig.bcrsdk.demo.Activity;

 import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.SurfaceTexture;
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.hardware.camera2.CaptureResult;
import android.hardware.camera2.TotalCaptureResult;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView; import com.intsig.bcrsdkdemo.R; import java.util.Arrays; @TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class Camera2Activity extends Activity {
private TextureView textureView;
private String TAG="测试"; private HandlerThread mThreadHandler;
private Handler mHandler; private CaptureRequest.Builder mPreviewBuilder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera2);
mThreadHandler = new HandlerThread("CAMERA2");
mThreadHandler.start();
mHandler = new Handler(mThreadHandler.getLooper()); textureView= (TextureView) findViewById(R.id.tv);
textureView.setSurfaceTextureListener(textureListener); } private TextureView.SurfaceTextureListener textureListener=new TextureView.SurfaceTextureListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
Log.e(TAG,"可用"); //CameraManaer 摄像头管理器,用于检测摄像头,打开系统摄像头
CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
try {
String[] CameraIdList=cameraManager.getCameraIdList();//获取可用相机列表
Log.e(TAG,"可用相机的个数是:"+CameraIdList.length);
CameraCharacteristics cameraCharacteristics=cameraManager.getCameraCharacteristics(CameraIdList[0]);//获取某个相机(摄像头特性)
cameraCharacteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);//检查支持 cameraManager.openCamera(CameraIdList[0],mCameraDeviceStateCallback, mHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
} } @Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
Log.e(TAG,"改变");
} @Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
Log.e(TAG,"释放");
return false;
} @Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
Log.e(TAG,"更新");
}
}; //CameraDeviceandroid.hardware.Camera也就是Camera1的Camera
private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
try {
startPreview(camera);
} catch (CameraAccessException e) {
e.printStackTrace();
}
} @Override
public void onDisconnected(CameraDevice camera) { } @Override
public void onError(CameraDevice camera, int error) { }
}; /**
* @param camera
* @throws CameraAccessException
* 开始预览
*/
private void startPreview(CameraDevice camera) throws CameraAccessException {
SurfaceTexture texture = textureView.getSurfaceTexture();
texture.setDefaultBufferSize(textureView.getWidth(), textureView.getHeight());
Surface surface = new Surface(texture);
try {
//CameraRequest表示一次捕获请求,用来对z照片的各种参数设置,比如对焦模式、曝光模式等。CameraRequest.Builder用来生成CameraRequest对象
mPreviewBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); } catch (CameraAccessException e) {
e.printStackTrace();
}
mPreviewBuilder.addTarget(surface);
camera.createCaptureSession(Arrays.asList(surface), mSessionStateCallback, mHandler);
} //CameraCaptureSession 这个对象控制摄像头的预览或者拍照
//setRepeatingRequest()开启预览,capture()拍照
//StateCallback监听CameraCaptureSession的创建
private CameraCaptureSession.StateCallback mSessionStateCallback = new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
Log.e(TAG,"相机创建成功!");
try {
session.capture(mPreviewBuilder.build(), mSessionCaptureCallback, mHandler);//拍照
session.setRepeatingRequest(mPreviewBuilder.build(), mSessionCaptureCallback, mHandler);//返回结果
} catch (CameraAccessException e) {
e.printStackTrace();
Log.e(TAG,"这里异常");
}
} @Override
public void onConfigureFailed(CameraCaptureSession session) {
Log.e(TAG,"相机创建失败!");
}
}; //CameraCaptureSession.CaptureCallback监听拍照过程
private CameraCaptureSession.CaptureCallback mSessionCaptureCallback = new CameraCaptureSession.CaptureCallback() { @Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
Log.e(TAG,"这里接受到数据"+result.toString());
} @Override
public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request, CaptureResult partialResult){ }};
}

Camera2与TextureView使用的更多相关文章

  1. Camera2使用textureView支持

    SurfaceView 绘制会有独立窗口, TextureView 没有独立的窗口,可以像普通的 View 一样,更高效更方便 public class MainActivity extends Ap ...

  2. Android Camera2 拍照(二)——使用TextureView

    原文:Android Camera2 拍照(二)--使用TextureView 上一篇博文简单介绍了使用Camera2 API拍摄照片,并使用SurfaceView作为预览界面.实际上,相对于Surf ...

  3. 摄像头(5)使用Camera2 替代过时的Camera API

    转自: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0428/2811.html 概要 从5.0开始(API Level 21 ...

  4. Android之TextureView浅析

    近期.在改动Android4.4的原生相机Camera2,非常习惯的去寻找SurfaceView.结果任凭我使用grep还是ack.都无法搜索到SurfaceView,最后还是通过代码CameraAc ...

  5. Android 用 camera2 API 自定义相机

    前言 笔者因为项目需要自定义相机,所以了解了一下 Android 关于 camera 这块的 API.Android SDK 21(LOLLIPOP) 开始已经弃用了之前的 Camera 类,提供了 ...

  6. Android Camera2采集摄像头原始数据并手动预览

    Android Camera2采集摄像头原始数据并手动预览 最近研究了一下android摄像头开发相关的技术,也看了Google提供的Camera2Basic调用示例,以及网上一部分代码,但都是在Te ...

  7. Android 视频展示控件之 SurfaceView、GLSurfaceView、SurfaceTexture、TextureView 对比总结

    一.SurfaceView SurfaceView继承自View,并提供了一个独立的绘图层,你可以完全控制这个绘图层,比如说设定它的大小,所以SurfaceView可以嵌入到View结构树中,需要注意 ...

  8. Android Camera2 预览功能实现

    1. 概述 最近在做一些关于人脸识别的项目,需要用到 Android 相机的预览功能.网上查阅相关资料后,发现 Android 5.0 及以后的版本中,原有的 Camera API 已经被 Camer ...

  9. Android流媒体开发之路一:Camera2采集摄像头原始数据并手动预览

    Android Camera2采集摄像头原始数据并手动预览 最近研究了一下android摄像头开发相关的技术,也看了Google提供的Camera2Basic调用示例,以及网上一部分代码,但都是在Te ...

随机推荐

  1. 如何用hexo搭建个人博客. 亲测有效

    搭建博客: 安装node.js和git 以管理员身份进入cmd. 输入:  npm install -g cnpm --registry=https://registry.npm.taobao.org ...

  2. leetcode笔记(七)529. Minesweeper

    题目描述 Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix repres ...

  3. 解决Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid" failed(2:No such file or directory)

    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

  4. Linux系统定时任务crond那些事

    1 Linux系统定时任务 1.1 定时任务介绍 1.1.1 Crond是什么? Crond是linux系统中用来定期执行命令或指定程序任务的一种服务或软件.Centos5/ linux系统安装完操作 ...

  5. ExtJS动态切换主题

    ExtJS动态切换主题         在Sencha Cmd构建的Ext程序中怎么去动态切换主题,目前看好像只能单一切换,但是在官网文档找到了答案 Resource Management在上一节通过 ...

  6. 构建ExtJS 6.x程序

    构建ExtJS 6.x程序 ExtJS也有自己的打包工具 SenchaCmd,它用来生成构建ExtJS前端组织架构,最后打包发布生产,操控着前端整个开发生命周期,SenchaCmd依赖于JDK,所以要 ...

  7. Asp.NET Core 在IIS部署 An assembly specified in the application dependencies manifest was not found

    今天在发布应用的时候,出来了一个报错:An assembly specified in the application dependencies manifest was not found 情况如下 ...

  8. Could not obtain transaction-synchronized Session for current thread 错误的解决方法!

    BsTable bsTable = new BsTable(); // String time = request.getParameter("date"); String tim ...

  9. 误删 EhCache 中的数据?

    最近遇到一个问题:在使用ehcache时,通过CacheManager.getCache(chachename).get(key),获取相应的缓存内对象(当时这个对象是个list), 有个同事写个方法 ...

  10. python学习之路2(程序的控制结构)

    1.程序的分支结构 1.1 单分支 if <条件>:                       例:guess = eval(input()) <语句块>          ...