SurfaceView预览camera
程序员文章站
2022-03-29 18:40:31
...
SurfaceView 预览Camera
实现步骤
1.增加权限
<uses-permission android:name="android.permission.CAMERA"/>
2.动态申请权限
//检查访问摄像头权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 100);
} else {
//拥有权限
initCameraView();
}
3.为SurfaceView的SurfaceHolder增加回调
类中要实现回调 implements SurfaceHolder.Callback
private void initCameraView() {
surfaceView = findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
}
4.Camera相机初始化并关联SurfaceHolder
记得加上这句camera.startPreview()
@Override
public void surfaceCreated(final SurfaceHolder holder) {
new Thread(new Runnable() {
@Override
public void run() {
try {
if (camera == null)
camera = Camera.open(1);
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
}
});
camera.setDisplayOrientation(90);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
上一篇: 文章标题
推荐阅读
-
官方下载:Win10最新预览版ISO镜像Build 18970和18363.327发布
-
Windows 10 18950预览版ISO镜像下载发布
-
Excel2010使用粘贴预览功能在复制粘贴过程中预览粘贴效果
-
还没打开就能看到内容如何取消Excel文件的预览功能
-
怎样才能把Word文字转成图片方便预览又不模糊
-
Win10手机预览版回滚WP8.1刷机工具更新下载:修复Bug
-
Win10 UWP版《Skype预览》迎更新:软件性能提升
-
Win10 Mobile秋季创意者更新15230预览版修复内容大全:Bug修复
-
Win10 Mobile预览版15235系统现新Bug: SD卡无法更新应用(附解决方法)
-
两个surfaceView实现切换效果