TextureView预览camera1API,可对TextureView进行旋转预览(旋转处理还没写)。
设置TextureView的SurfaceTextureListener()
textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
openCamera(surface);
}
@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 void openCamera(SurfaceTexture surface) {
if (Camera.getNumberOfCameras() == 0){
Log.d(TAG,"未检出到摄像头");
return;
}
try {
camera = Camera.open();
camera.setPreviewTexture(surface);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
另外起个线程连续读取预览的Bitmap画面:
new Thread(new Runnable() {
@Override
public void run() {
while (true){
SystemClock.sleep(50);
iv_bitmap_preview.post(new Runnable() {
@Override
public void run() {
iv_bitmap_preview.setImageBitmap(textureView.getBitmap());
}
});
}
}
}).start();