前端时间有个需求是设备上安装2个USB摄像头实现平台远程拍照功能,需要无界面拍照,就是静默拍照,偷拍那种,在网上看了好多博客,现在整理一下:
1,android调用相机拍照,有预览界面,直接把预览界面的宽高都设置为0.1dp,直接上代码:
在AndroidManifest.xml中加入权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
然后就是直接一个线程就好了,在子线程中执行:
public class Camera1Run implements Runnable {
private Camera myCamera;
private final Context mContext;
public Camera1Run(Context mContext) {
this.mContext = mContext;
}
@Override
public void run() {
initCamera();
}
// 初始化摄像头
private void initCamera() {
// 如果存在摄像头
if (checkCameraHardware(mContext)) {
// 获取摄像头(首选前置,无前置选后置)
if (openFacingFrontCamera()) {
Log.i("Camera1Run", "openCameraSuccess");
// 进行对焦
autoFocus();
} else {
Log.i("Camera1Run", "openCameraFailed");
}
}
}
// 对焦并拍照
private void autoFocus() {
try {
// 因为开启摄像头需要时间,这里让线程睡两秒
Thread.sleep(500);
Log.i("测试线程拍照", Thread.currentThread().getId() + "");
// 自动对焦
myCamera.autoFocus(myAutoFocus);
// 对焦后拍照
myCamera.takePicture(null, null, myPicCallback);
} catch (Exception e) {
e.printStackTrace();
}
}
// 判断是否存在摄像头
private boolean checkCameraHardware(Context context) {
// 设备存在摄像头
// 设备不存在摄像头
return context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA);
}
// 得到后置摄像头
private boolean openFacingFrontCamera() {
// 尝试开启前置摄像头
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
for (int camIdx = 0, cameraCount = Camera.getNumberOfCameras(); camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
Log.i("Camera1Run", "tryToOpenCamera");
myCamera = Camera.open(camIdx);
} catch (RuntimeException e) {
e.printStackTrace();
return false;
}
}
}
// 如果开启前置失败(无前置)则开启后置
if (myCamera == null) {
for (int camIdx = 0, cameraCount = Camera.getNumberOfCameras(); camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
try {
myCamera = Camera.open(camIdx);
} catch (RuntimeException e) {
return false;
}
}
}
}
/*try { // 这里的myCamera为已经初始化的Camera对象 myCamera.setPreviewDisplay(myHolder); } catch (IOException e) { e.printStackTrace(); myCamera.stopPreview(); myCamera.release(); myCamera = null; } */
myCamera.startPreview();
return true;
}
// 自动对焦回调函数(空实现)
private final Camera.AutoFocusCallback myAutoFocus = new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
}
};
// 拍照成功回调函数
private final Camera.PictureCallback myPicCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
String mFilePath = null;
FileOutputStream fos = null;
try {
mFilePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "100.jpeg";
//文件
File tempFile = new File(mFilePath);
if (tempFile.exists()) {
tempFile.delete();
}
// tempFile.createNewFile();
fos = new FileOutputStream(tempFile);
fos.write(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
//实现连续拍多张的效果
// mCamera.startPreview();
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
myCamera.stopPreview();
myCamera.release();
myCamera = null;
Log.i("Camera1Run", "拍照完成");
if (isFileExist(mFilePath)) {
Log.i("Camera1Run", "上传图片");
request6(mFilePath);//图片上传服务器
}
}
// Log.i("测试线程处理拍照", Thread.currentThread().getId() + "");
// // 将得到的照片进行270°旋转,使其竖直
// Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
// Matrix matrix = new Matrix();
// matrix.preRotate(270);
// bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
// bitmap.getHeight(), matrix, true);
//
// String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
// .format(new Date());
//
// // 创建并保存图片文件
// File pictureFile = new File(getDir(), "IMG_" + timeStamp + ".jpg");
//
// NativeUtil.compressBitmap(bitmap, pictureFile.getPath());
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 80, fos);
fos.close();
} catch (Exception error) {
Toast.makeText(getActivity(), "拍照失败", Toast.LENGTH_SHORT)
.show();
;
Log.i(TAG, "保存照片失败" + error.toString());
error.printStackTrace();
myCamera.stopPreview();
myCamera.release();
myCamera = null;
}
//
// Log.i("Camera1Run", "获取照片成功");
// Toast.makeText(mContext, "获取照片成功", Toast.LENGTH_SHORT).show();
// myCamera.stopPreview();
// myCamera.release();
// myCamera = null;
}
};
// 获取文件夹
private File getDir() {
// 得到SD卡根目录
File dir = Environment.getExternalStorageDirectory();
if (!dir.exists()) {
dir.mkdirs();
}
return dir;
}
}
就上面这一个子线程就搞定了