如何调用摄像头拍照
创建布局文件
以为每次找CSDN 总是找不到全部的代码,所以我就直接全部粘贴下来了
// 创建布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<!--用于显示预览-->
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--LinearLayout线性布局管理器 放在父容器底部-->
<!--LinearLayout线性布局管理器 内部水平分布-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:gravity="center_horizontal">
<Button
android:id="@+id/take_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍照" />
<Button
android:id="@+id/preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开摄像头" />
</LinearLayout>
</RelativeLayout>
主函数方法
1.这里调用了Camera 的类
2.声明一个布尔型变量ispreview,用来判断摄像头是否打开
//写在开始部分
private Camera camera;
private boolean ispreview = false;
- 设置全屏显示
//写在onCreat方法里面
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
4.获取SurfaceView,SurfaceHolder
//写在onCreat方法里面
final SurfaceView surface = findViewById(R.id.surfaceview);//获取surfaceview
final SurfaceHolder surfaceHolder = surface.getHolder();//获取suafaceholder
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- 获取监听预览按钮
//写在onCreat方法里面
Button preview = findViewById(R.id.preview);
preview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!ispreview) {//如果没打开
camera = Camera.open();//打开摄像头
ispreview = true;
}
try {
camera.setPreviewDisplay(surfaceHolder);//设置预览
Camera.Parameters parameters = camera.getParameters();//获取摄像头参数
parameters.setPictureFormat(PixelFormat.JPEG);//设置图片为jpg
parameters.set("jpeg-quality", 80);//设置图片质量
camera.setParameters(parameters);//重新设置摄像头参数
camera.startPreview();//开始预览
camera.setDisplayOrientation(90);//不加的话,预览的图像就是横的
camera.autoFocus(null);//自动对焦
} catch (IOException e) {
e.printStackTrace();
}
}
});
6.拍照
//写在onCreat方法里面
Button take_photo = (Button)findViewById(R.id.take_photo);//获取按钮
take_photo.setOnClickListener(new View.OnClickListener() {//监听
@Override
public void onClick(View v) {
if (camera != null) {
camera.takePicture(null, null, jpeg);
}
}
});
- 存储图像
//写在onCreat方法外面
final Camera.PictureCallback jpeg = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
//根据拍照得到的数据集创建位图
camera.stopPreview();
ispreview = false;
File appDir = new File(Environment.getExternalStorageDirectory(), "/DCIM/Camera/");
if (!appDir.exists()) {//如果目录不存在
appDir.mkdir();//创建目录
}
String filename = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, filename);
try {
FileOutputStream outputStream = new FileOutputStream(file);
//创建文件输出流
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
//将图片压缩成JPEG格式输出到输出流
outputStream.flush();//将缓冲区的数据都输入到输出流
outputStream.close();//关闭输出流
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//将图片插入到系统图库
try {
MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), file.getAbsolutePath(), filename, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//通知图库更新
MainActivity.this.sendBroadcast(new Intent(Intent.
ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+"")));
Toast.makeText(MainActivity.this,"照片已存"+file,Toast.LENGTH_LONG).show();
resetCamera();//图片保存后,判断,是否需要重新打开预览,重新创建一个方法,第8步
}
};
8.重新判定摄像头是否在预览
private void resetCamera() {
if (!ispreview) {
camera.startPreview();
ispreview=true;
}
}
- 释放摄像头资源
protected void onPause() {
super.onPause();
if(camera!=null){
camera.stopPreview();
camera.release();
}
}
10.从这边开始,我们就完成了整体布局,下面去manifest文件中进行权限处理
路径为:app/manifes/AndroidManifest.xml , 这是安卓布局文件
11. 增加了 摄像头权限,读写权限,自动对焦 的申请
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofoucs"/>
12.最后可以真机运行了,运行之前,记得先把权限打开
一定要先给权限,不然会闪退
13. 可运行的,我的手机是米9 Android9,API28
github链接
学习一门视频的总结,不知道算不算原创,单纯写一下