什么是Camera2
Google在2014年发布了Android5.0 , 在Android5.0的API中我们可以发现,以往的Camera类已经被遗弃,不再建议使用,如果继续在5.0以后的系统版本中使用Camera类开发相机,会出现各种各样的兼容问题,那么我们该怎么办呢?
不必担心,Google在新的API中为我们提供了新的相机类,Camera2。该类能够提供更丰富的功能以及更高的性能。相对的,因为全程异步的关系,它的使用难度也有所提高,并且网络上相关的中文教程并不多。下面的示例代码为使用Camera2实现预览与闪关灯的功能。
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class FlashActivity extends Activity implements TextureView.SurfaceTextureListener,Runnable {
private ImageButton mSwitchBtn;
private boolean isOpen;
private CameraManager mCameraManager;
private TextureView mPreviewView;
private Handler mHandler;
private CameraCaptureSession mSession;
private HandlerThread mHandlerThread;
private Size mPreviewSize;
private CaptureRequest.Builder mPreviewBuilder;
private boolean sos;
private TextView mSosBtn;
private TextView mScreenBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash);
init();
initLooper();
bindEvents();
}
/**
* 初始化控件
*/
private void init() {
mSwitchBtn = (ImageButton) findViewById(R.id.switchBtn);
mPreviewView = (TextureView) findViewById(R.id.texture);
mSosBtn = (TextView) findViewById(R.id.sosBtn);
mScreenBtn = (TextView) findViewById(R.id.screenBtn);
mPreviewView.setSurfaceTextureListener(this);
}
private void bindEvents() {
mSwitchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isOpen) {
openFlash();
isOpen = true;
mSwitchBtn.setBackgroundResource(R.mipmap.open);
} else {
closeFlash();
isOpen = false;
mSwitchBtn.setBackgroundResource(R.mipmap.close);
}
}
});
mSosBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!sos) {
sos = true;
mSosBtn.setText("SOS ON");
} else {
sos = false;
mSosBtn.setText("SOS OFF");
}
new Thread(FlashActivity.this).start();
}
});
mScreenBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(FlashActivity.this, ColorActivity.class));
}
});
}
private void closeFlash() {
mPreviewBuilder.set(CaptureRequest.FLASH_MODE,
CaptureRequest.FLASH_MODE_OFF);
updatePreview(mSession);
}
private void openFlash() {
mPreviewBuilder.set(CaptureRequest.FLASH_MODE,
CaptureRequest.FLASH_MODE_TORCH);
updatePreview(mSession);
}
//需要一个子线程的looper,因为camera2是全程异步的
public void initLooper() {
mHandlerThread = new HandlerThread("Camera2");
mHandlerThread.start(); //开启handle线程
mHandler = new Handler(mHandlerThread.getLooper());//用handler线程中获取的looper,实例化一个handler
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
//获得CameraManager
mCameraManager = (CameraManager) this.getSystemService(CAMERA_SERVICE);
//获得属性
CameraCharacteristics characteristics = null;
try {
characteristics = mCameraManager.getCameraCharacteristics(0 + "");
} catch (CameraAccessException e) {
e.printStackTrace();
}
//支持的STREAM CONFIGURATION
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
//显示的size
mPreviewSize = map.getOutputSizes(SurfaceTexture.class)[0];
//打开相机
try {
mCameraManager.openCamera(0 + "", mCameraDeviceStateCallback, mHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
startPreview(camera); //在相机被打开的回调里 调用startPreview传进相机对象 开始预览
}
@Override
public void onDisconnected(CameraDevice camera) {
}
@Override
public void onError(CameraDevice camera, int error) {
}
};
private void startPreview(CameraDevice camera) {
//获取SurfaceTexture对象用于预览相机图像
SurfaceTexture surfaceTexture = mPreviewView.getSurfaceTexture();
//设置预览的尺寸
surfaceTexture.setDefaultBufferSize(mPreviewView.getWidth(), mPreviewView.getHeight());
Surface surface = new Surface(surfaceTexture);
try {
//获得图像信息,参数代表是预览图像
mPreviewBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewBuilder.addTarget(surface); //设置页面预览的位置
//创建会话
camera.createCaptureSession(Arrays.asList(surface), mSessionStateCallback, mHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private CameraCaptureSession.StateCallback mSessionStateCallback = new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
mSession = session; //将session对象,作用域扩大至全局,以便开关闪光灯使用
updatePreview(session);//更新信息
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
}
};
private void updatePreview(CameraCaptureSession session) {
try {
session.setRepeatingRequest(mPreviewBuilder.build(), null, mHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (sos) {
try {
Thread.sleep(200); //线程休眠
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!isOpen) {
openFlash();
isOpen = true;
} else {
closeFlash();
isOpen = false;
}
}
} }