Android实现极简打开摄像头
作者:哒哒呵 时间:2022-09-10 15:56:51
很多时候忘记Android摄像头如何打开,查看google文档的话,发现太复杂(只是单纯的想打开摄像头而已,不想添加那么多设置,添加那么功能),很多博客也是对官方文档的小修小改,连方法名都一样,因此,我决定完成Android相机最简单的打开摄像头(仅仅打开)。很久没用忘掉的话,打开链接复制粘贴一下就完事了。
AndroidManifest.xml设置CAMERA权限后,在代码中还要设置权限检查,但是因为我连权限检查都懒得加了,装好后直接在手机系统里手动允许权限。
Camera1(已废弃):
xml中使用SurfaceView作为预览View
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity:
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback{
private SurfaceHolder holder;
private Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
SurfaceView surfaceView = findViewById(R.id.surfaceView);
holder = surfaceView.getHolder();
holder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if(camera == null){
try {
camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
camera.setPreviewDisplay(holder);
camera.startPreview();
Camera.Parameters parameters = camera.getParameters();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
camera.setParameters(parameters);
camera.setDisplayOrientation(90);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
这样就完成了最简单的打开摄像头并在手机中出现画面。(代码里去掉2个接口中未实现的方法)
Camera2
Android 5.0(API 21)以后,谷歌就决定废弃原有的Camera API改用Camera2 API,因为功能更强大
xml使用TextureView作为预览(其实SurfaceView也行,官方的Demo是用TextureView的一个子类):
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextureView
android:id="@+id/surfaceView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity:
public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {
private TextureView textureView;
private CaptureRequest.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textureView = findViewById(R.id.surfaceView);
textureView.setSurfaceTextureListener(this);
}
@Override
public void onSurfaceTextureAvailable(final SurfaceTexture surface, int width, int height) {
CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE);
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
try {
manager.openCamera("0", new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
try {
SurfaceTexture surfaceTexture = textureView.getSurfaceTexture();
surfaceTexture.setDefaultBufferSize(1440,1080);
Surface surface = new Surface(surfaceTexture);
builder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
builder.addTarget(surface);
camera.createCaptureSession(Arrays.asList(surface),
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
try {
session.setRepeatingRequest(builder.build(), null, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
}
@Override
public void onError(@NonNull CameraDevice camera, int error) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}
这样就成功使用Camera2的API打开并预览了(代码里去掉3个接口中未实现的方法)
来源:https://blog.csdn.net/weixin_45253393/article/details/105323765
标签:Android,摄像头
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Springboot项目引入druid安装部署使用教程
2023-07-28 01:39:25
![](https://img.aspxhome.com/file/2023/5/57625_0s.png)
Android SwipeRefreshLayout超详细讲解
2023-08-22 13:43:28
![](https://img.aspxhome.com/file/2023/7/94257_0s.gif)
android 获取视频,图片缩略图的具体实现
2023-07-28 00:20:39
Java使用JDK与Cglib动态代理技术统一管理日志记录
2021-11-09 00:52:31
列举java语言中反射的常用方法及实例代码
2022-10-31 13:45:07
Java使用通配符实现增强泛型详解
2021-07-06 17:28:01
![](https://img.aspxhome.com/file/2023/2/60282_0s.png)
Java实现聊天室界面
2023-12-15 10:43:32
Spring中XML schema扩展机制的深入讲解
2022-06-29 07:44:15
![](https://img.aspxhome.com/file/2023/4/77384_0s.png)
Android开发之android_gps定位服务简单实现
2023-07-31 20:02:25
![](https://img.aspxhome.com/file/2023/3/83553_0s.gif)
springboot调用支付宝第三方接口(沙箱环境)
2023-11-25 06:12:08
![](https://img.aspxhome.com/file/2023/8/58748_0s.png)
git忽略特殊文件_动力节点Java学院整理
2023-02-26 19:27:47
![](https://img.aspxhome.com/file/2023/0/94350_0s.jpg)
Android CoordinatorLayout高级用法之自定义Behavior
2022-03-20 05:47:51
![](https://img.aspxhome.com/file/2023/3/91673_0s.gif)
SpringCloud Eureka服务治理之服务注册服务发现
2021-12-27 15:07:16
![](https://img.aspxhome.com/file/2023/0/79950_0s.png)
SQLite在C#中的安装与操作技巧
2023-04-19 08:31:12
![](https://img.aspxhome.com/file/2023/7/75587_0s.png)
opencv实现轮廓高斯滤波平滑
2023-07-23 02:05:56
![](https://img.aspxhome.com/file/2023/4/82534_0s.jpg)
基于springboot实现redis分布式锁的方法
2023-06-16 01:36:56
![](https://img.aspxhome.com/file/2023/5/82365_0s.png)
spring boot整合redis主从sentinel方式
2021-10-01 20:13:47
![](https://img.aspxhome.com/file/2023/7/68227_0s.png)
Java编程实现中英混合字符串数组按首字母排序的方法
2022-03-16 02:34:54
java ThreadPool线程池的使用,线程池工具类用法说明
2023-06-23 18:12:12
DevExpress中GridControl列转义的实现方法
2022-11-10 05:14:31
![](https://img.aspxhome.com/file/2023/8/68078_0s.png)