Android调用外置摄像头的方法
作者:北极熊的微笑 时间:2021-10-19 01:25:13
本文实例为大家分享了Android调用外置摄像头的具体代码,供大家参考,具体内容如下
1、布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextureView
android:id="@+id/textureview"
android:layout_width="1dp"
android:layout_height="1dp"/>
<ImageButton
android:id="@+id/play"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:background="@drawable/ic_launcher_background"
android:contentDescription="@string/app_name"
android:layout_marginBottom="10dp"/>
</RelativeLayout>
2、相应的MainActivity.java的主要代码如下
package com.deepreality.takephotowithusbcamera;
import android.Manifest;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.TextureView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import com.tbruyelle.rxpermissions2.RxPermissions;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener, View.OnClickListener {
private static final String TAG = MainActivity.class.getSimpleName();
private Camera mCamera;
private ImageButton mPlayButton;
private RxPermissions rxPermissions;
private int permissionNum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rxPermissions = new RxPermissions(MainActivity.this);
checkUserAllPermissions();
mPlayButton = (ImageButton) findViewById(R.id.play);
mPlayButton.setOnClickListener(this);
((TextureView) findViewById(R.id.textureview))
.setSurfaceTextureListener(this);
}
private void takePic() {
if (mCamera != null) {
//调用抓拍摄像头抓拍
mCamera.takePicture(null, null, pictureCallback);
} else {
Log.e("TAG", "请检查摄像头!");
}
}
private Bitmap mBitmap;
public Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.i("ygy", "onPictureTaken");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
String picName = df.format(new Date());
Toast.makeText(getApplicationContext(), "正在保存...", Toast.LENGTH_LONG).show();
mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
File file = new File("/storage/emulated/0/" + picName + ".jpg");
try {
file.createNewFile();
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
Toast.makeText(getApplicationContext(), "图像保存成功", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
};
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mCamera = Camera.open(0);
if (mCamera != null) {
try {
mCamera.setPreviewTexture(surface);
mCamera.startPreview();
} catch (IOException e) {
Log.d("TAG", e.getMessage());
}
}
}
@Override
protected void onStop() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
super.onStop();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
@Override
public void onClick(View v) {
if (mCamera == null) {
return;
}
takePic();
}
/**
* 检查并获取用户权限
*/
private void checkUserAllPermissions() {
rxPermissions
.requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
)
.subscribe(permission -> {
if (permission.granted) {
} else if (permission.shouldShowRequestPermissionRationale) {
} else {}
permissionNum ++;
if (permissionNum == 2) {
}
});
}
}
3、注意在清单文件里AndroidManifest.xml添加用户权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
来源:https://blog.csdn.net/lpCrazyBoy/article/details/89314512
标签:Android,摄像头
0
投稿
猜你喜欢
JNI方法实现图片压缩(压缩率极高)
2021-08-07 11:32:55
Java使用雪花id生成算法详解
2023-11-18 21:58:49
Android自定义view实现有header和footer作为layout使用的滚动控件
2023-07-31 19:29:54
Android程序启动时出现黑屏问题的解决方法
2022-07-06 05:16:39
如何利用java控制鼠标操作一些重复的事情
2021-11-12 12:54:02
Android Fragment源码分析Add方法
2023-02-12 19:13:58
在springboot中如何使用filter设置要排除的URL
2022-11-06 00:06:01
Android中使用AspectJ详解
2022-01-29 19:12:05
用C#生成不重复的随机数的代码
2023-03-08 03:36:52
详解Java ES多节点任务的高效分发与收集实现
2021-08-03 13:59:02
Java多线程之synchronized关键字的使用
2023-12-12 21:46:16
Java中ArrayList和LinkedList之间的区别_动力节点Java学院整理
2023-03-30 18:16:29
Android 自定义view实现进度条加载效果实例代码
2022-12-21 05:33:48
C#转换日期类型实例
2023-07-16 21:01:19
Unity实现识别图像中主体及其位置
2022-03-04 09:48:56
Mybatis批量修改时出现报错问题解决方案
2022-05-15 19:07:40
如何使用RequestHeaders添加自定义参数
2023-01-10 17:37:14
基于FeignException$InternalServerError的解决方案
2023-04-25 15:50:45
java实现打砖块小游戏
2021-07-26 14:47:11
Android SimpleAdapter适配器使用详解
2023-10-07 07:04:26