Android CameraManager 类
先看代码:
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private CameraManager cameraManager; cameraManager = new CameraManager(this);
mSurfaceView = (SurfaceView) findViewById(R.id.java_surface_view);
mSurfaceHolder = mSurfaceView.getHolder(); // mSurfaceView 不需要自己的缓冲区
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// mSurfaceView添加回调
mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) { //SurfaceView创建
try {
cameraManager.openDriver(mSurfaceHolder);
cameraManager.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
} @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override
public void surfaceDestroyed(SurfaceHolder holder) { //SurfaceView销毁
holder.removeCallback(this); // Camera is being used after Camera.release() was called
cameraManager.stopPreview();
cameraManager.closeDriver(); }
});
自定义一个 CameraManager 类,在关闭当前页面,释放camera资源时偶尔报错
Camera is being used after Camera.release() was called
第一次,加了一句 holder.removeCallback(this); 运行发现还是时不时出现报错。
第二次在释放 camera前加 camera.setPreviewCallback(null); 问题解决。
/**
* 关闭预览
*/
public synchronized void stopPreview() {
Log.e(TAG, "stopPreview");
if (autoFocusManager != null) {
autoFocusManager.stop();
autoFocusManager = null;
}
if (camera != null && previewing) {
camera.stopPreview();
camera.setPreviewCallback(null); // Camera is being used after Camera.release() was called
previewing = false;
}
} /**
* 关闭camera
*/
public synchronized void closeDriver() {
Log.e(TAG, "closeDriver");
if (camera != null) {
camera.release();
camera = null;
}
}
原来:如果程序中加入了previewCallback,在surfaceDestroy释放camera的时候,最好执行myCamera.setOneShotPreviewCallback(null); 或者myCamera.setPreviewCallback(null);中止这种回调,然后再释放camera更安全。否则可能会报错。
附:CameraManager类:
package com.myproject.facedetection.entity; import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.WindowManager; import com.opencvlib.ObjectDetector; import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Rect;
import org.opencv.imgproc.Imgproc; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import static com.myproject.facedetection.common.myUtils.ByteToBitmap; /**
* User:lizhangqu(513163535@qq.com)
* Date:2015-09-05
* Time: 10:56
*/
public class CameraManager implements Camera.PreviewCallback {
private static final String TAG = CameraManager.class.getName();
private Camera camera;
private Camera.Parameters parameters;
private AutoFocusManager autoFocusManager;
private int requestedCameraId = -1; // 默认打开后置摄像头
private int cameraPosition;//0代表前置摄像头,1代表后置摄像头
private int numCameras = Camera.getNumberOfCameras(); // 初始化摄像头数量
private Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
private Context mContext; private boolean initialized;
private boolean previewing;
private ArrayList<ObjectDetector> mObjectDetects;
private CustomImageButton cimbt;
private SurfaceHolder holder; /**
* 打开摄像头
*
* @param cameraId 摄像头id
* @return Camera
*/
public Camera open(int cameraId) {
if (numCameras <= 0) {
Log.e(TAG, "No cameras!");
return null;
} // 如果 cameraId 在 [0-numCameras) 之间则打开cameraId 否则默认优先打开后置
boolean explicitRequest = (cameraId >= 0 && cameraId < numCameras); int index = 0;
if (!explicitRequest) {
// Select a camera if no explicit camera requested
while (index < numCameras) {
Camera.getCameraInfo(index, cameraInfo);
cameraId = index;
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
cameraPosition = 1;
break;
}
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
cameraPosition = 0;
continue;
} index++;
}
} // 不在 [0-numCameras) 之间 且 没有默认打开的
Camera camera;
if (index < numCameras) {
Log.e(TAG, "Opening camera #" + cameraId);
camera = Camera.open(cameraId);
} else {
Log.e(TAG, "Requested camera does not exist: " + cameraId);
camera = null;
} int rotation = getDisplayOrientation();
camera.setDisplayOrientation(rotation);
camera.setPreviewCallback(this);
//camera.setOneShotPreviewCallback(this); // 激活 onPreviewFrame 执行一次
return camera;
} public int getDisplayOrientation() {
Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
Camera.CameraInfo camInfo = new Camera.CameraInfo();
Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, camInfo);
int result = (camInfo.orientation - degrees + 360) % 360;
return result;
} public CameraManager(Context context, ArrayList<ObjectDetector> mObjectDetects, CustomImageButton cimbt, SurfaceHolder holder) {
this.mContext = context;
this.mObjectDetects = mObjectDetects;
this.cimbt = cimbt;
this.holder = holder;
} /**
* 打开camera
*
* @throws IOException IOException
*/
public synchronized void openDriver()
throws IOException {
Log.e(TAG, "openDriver");
Camera theCamera = camera;
if (theCamera == null) {
theCamera = open(requestedCameraId);
if (theCamera == null) {
throw new IOException();
}
camera = theCamera;
}
theCamera.setPreviewDisplay(holder); if (!initialized) {
initialized = true;
parameters = camera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes(); int w = 800;
int h = 600;
for (Camera.Size size : previewSizes) {
Log.e("TAG", "previewSizes width:" + size.width);
Log.e("TAG", "previewSizes height:" + size.height);
if (size.width - w <= 200 & size.width >= w) {
w = size.width;
h = size.height;
break;
}
}
Log.e("w*h", String.format("%d*%d", w, h));
parameters.setPreviewSize(w, h);
parameters.setPictureFormat(ImageFormat.JPEG);
parameters.setJpegQuality(100);
parameters.setPictureSize(800, 600);
theCamera.setParameters(parameters);
}
} /*
切换摄像头
*/
public synchronized void changeCamera() throws IOException {
for (int i = 0; i < numCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);//得到每一个摄像头的信息
//现在是后置,变更为前置
if (cameraPosition == 1) {
//代表摄像头的方位 CAMERA_FACING_FRONT前置 CAMERA_FACING_BACK后置
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
stopPreview();
closeDriver(); requestedCameraId = i;
initialized = false; openDriver();
startPreview();
cameraPosition = 0;
break;
}
} else {
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
stopPreview();
closeDriver(); requestedCameraId = i;
initialized = false; openDriver();
startPreview();
cameraPosition = 1;
break;
}
} }
} @Override
public void onPreviewFrame(byte[] bytes, Camera camera) {
Camera.Size previewSize = camera.getParameters().getPreviewSize(); Bitmap bitmap = ByteToBitmap(bytes, previewSize);
//Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);//将data byte型数组转换成bitmap文件 final Matrix matrix = new Matrix();//转换成矩阵旋转90度
if (cameraPosition == 1) {
matrix.setRotate(90);
} else {
matrix.setRotate(-90);
}
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);//旋转图片 Mat grayscaleImage = new Mat(previewSize.height, previewSize.width, CvType.CV_8UC4);
int absoluteFaceSize = (int) (previewSize.height * 0.2); if (bitmap != null) {
Mat inputFrame = new Mat();
Utils.bitmapToMat(bitmap, inputFrame); if (!bitmap.isRecycled()) {
bitmap.recycle();
} // Create a grayscale image
Imgproc.cvtColor(inputFrame, grayscaleImage, Imgproc.COLOR_RGBA2RGB); MatOfRect mRect = new MatOfRect(); int maxRectArea = 0 * 0;
Rect maxRect = null; int facenum = 0; for (ObjectDetector detector : mObjectDetects) {
// 检测目标
Rect[] object = detector.detectObjectImage(inputFrame, mRect);
Log.e(TAG, object.length + ""); for (Rect rect : object) {
++facenum;
// 找出最大的面积
int tmp = rect.width * rect.height;
if (tmp >= maxRectArea) {
maxRectArea = tmp;
maxRect = rect;
}
}
} Bitmap rectBitmap = null;
if (facenum != 0) {
// 剪切最大的头像
//Log.e("剪切的长宽", String.format("高:%s,宽:%s", maxRect.width, maxRect.height));
Rect rect = new Rect(maxRect.x, maxRect.y, maxRect.width, maxRect.height);
Mat rectMat = new Mat(inputFrame, rect); // 从原始图像拿
rectBitmap = Bitmap.createBitmap(rectMat.cols(), rectMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rectMat, rectBitmap); Bitmap resizeBmp = cimbt.resizeBitmap(rectBitmap, cimbt.getWidth(), cimbt.getHeight());
cimbt.setBitmap(resizeBmp);
} else {
cimbt.clearnImage();
cimbt.setText("没有检测到人脸");
}
} } public interface OnObjectTrackingInterface {
// 传输camera的字节流
void onCameraByteStream(byte[] bytes, Camera camera); } /**
* camera是否打开
*
* @return camera是否打开
*/ public synchronized boolean isOpen() {
return camera != null;
} /**
* 关闭camera
*/
public synchronized void closeDriver() {
Log.e(TAG, "closeDriver");
if (camera != null) {
camera.release();
camera = null;
}
} /**
* 开始预览
*/
public synchronized void startPreview() {
Log.e(TAG, "startPreview");
Camera theCamera = camera;
if (theCamera != null && !previewing) {
theCamera.startPreview();
previewing = true;
autoFocusManager = new AutoFocusManager(camera);
}
} /**
* 关闭预览
*/
public synchronized void stopPreview() {
Log.e(TAG, "stopPreview");
if (autoFocusManager != null) {
autoFocusManager.stop();
autoFocusManager = null;
}
if (camera != null && previewing) {
camera.stopPreview();
camera.setPreviewCallback(null); // Camera is being used after Camera.release() was called
previewing = false;
}
} /**
* 打开闪光灯
*/
public synchronized void openLight() {
Log.e(TAG, "openLight");
if (camera != null) {
parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
}
} /**
* 关闭闪光灯
*/
public synchronized void offLight() {
Log.e(TAG, "offLight");
if (camera != null) {
parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
}
} /**
* 拍照
*
* @param shutter ShutterCallback
* @param raw PictureCallback
* @param jpeg PictureCallback
*/
public synchronized void takePicture(final Camera.ShutterCallback shutter, final Camera.PictureCallback raw,
final Camera.PictureCallback jpeg) {
camera.takePicture(shutter, raw, jpeg);
} private Bitmap reSize(byte[] data) {
Log.i(TAG, "myJpegCallback:onPictureTaken...");
Bitmap cutMap = BitmapFactory.decodeByteArray(data, 0, data.length);//data是字节数据,将其解析成位图
//设置FOCUS_MODE_CONTINUOUS_VIDEO)之后,myParam.set("rotation", 90)失效。图片竟然不能旋转了,故这里要旋转下
Matrix matrix = new Matrix();
matrix.postRotate((float) 90.0);
Bitmap rotaBitmap = Bitmap.createBitmap(cutMap, 0, 0, cutMap.getWidth(), cutMap.getHeight(), matrix, false); //旋转后rotaBitmap是960×1280.预览surfaview的大小是540×800
//将960×1280缩放到540×800
Bitmap sizeBitmap = Bitmap.createScaledBitmap(rotaBitmap, 540, 800, true);
Bitmap rectBitmap = Bitmap.createBitmap(sizeBitmap, 100, 200, 300, 300);//截取
return rectBitmap;
}
}
CameraManager
Android CameraManager 类的更多相关文章
- android Activity类中的finish()、onDestory()和System.exit(0) 三者的区别
android Activity类中的finish().onDestory()和System.exit(0) 三者的区别 Activity.finish() Call this when your a ...
- Android图像格式类及图像转换方法
Android图像格式类及图像转换方法介绍 一款软件的开发和图像密切相关,特别是移动应用程序,在视觉效果等方面是至关重要的,因为这直接关系到用户的体验效果.在Android程序开发的过程中,了解存在哪 ...
- Android 服务类Service 的详细学习
http://blog.csdn.net/vipzjyno1/article/details/26004831 Android服务类Service学习四大组建 目录(?)[+] 什么是服务 服务有 ...
- 项目源码--Android答题类游戏源码
下载源码 技术要点: 1. 精致的答题UI 2. Android的Http通信技术 3. Android数据库QLITE与其他数据存储技术 4. Android在线音乐背景技术 5. Android答 ...
- android application类的用法
android application类的用法 Application是android系统Framework提供的一个组件,它是单例模式(singleton),即每个应用只有一个实例,用来存储系统的一 ...
- Android Matrix类以及ColorMatri
引自:http://www.chinabaike.com/t/37396/2014/0624/2556217.html Android Matrix类以及ColorMatrix类详解 最近在系统学习了 ...
- android资讯类软件框架《IT蓝豹》
android资讯类软件框架 android资讯类软件框架,支持侧滑,并且首页viewpager切换tab,tab滑动到最右侧的时候提示滑动结束, 滑动到最左侧的时候切换滑动侧滑menu.左滑和侧滑处 ...
- [Android Pro] 常用的android工具类和库
reference to : http://blog.csdn.net/lovexieyuan520/article/details/50614086 这篇博客主要记录我认为比较有用的Android ...
- 如何使用Android JetPlayer类
在Android中,还提供了对Jet播放的支持,Jet是由OHA联盟成员SONiVOX开发的一个交互音乐引擎.其包括两部分:JET播放器和JET引擎.JET常用于控制游戏的声音特效,采用MIDI(Mu ...
随机推荐
- 利用window.performance.timing进行性能分析
性能分析... window.performance.timing中相关属性语义: // .navigationStart 准备加载页面的起始时间 // .unloadEventStart 如果前一个 ...
- MySQL索引管理
一.索引介绍 1.什么是索引 1.索引好比一本书的目录,它能让你更快的找到自己想要的内容. 2.让获取的数据更有目的性,从而提高数据库索引数据的性能. 2.索引类型介绍 1.BTREE:B+树索引 2 ...
- BZOJ 3669 魔法森林
LCT维护生成树 先按照a的权值把边排序,离线维护b的最小生成树. 将a排序后,依次动态加边,我们只需要关注b的值.要保证1-n花费最少,两点间的b值肯定是越小越好,所以我们可以考虑以b为关键字维护最 ...
- 来自多校的一个题——数位DP+卡位
n<=1e9就要考虑倍增.矩阵乘法这种了 假设L=0 考虑枚举二进制下,所有X与R的LCP长度,前len高位 对于第len+1位,假设R的这一位是1 如果一个x的这一位是0了,那么后面可以随便填 ...
- https://www.chromestatus.com/features/5093566007214080
移动端滑动报错:Unable to preventDefault inside passive event listener due to target being treated as passiv ...
- NOI-OJ 1.13 ID:34 确定进制
整体思路 对于任意的p,q,r,可能使得p*q=r的最小进制应该是p,q,r三个数的所有数位中最大的数字+1,例如,6,9,42三个数中所有数位中最大的数字是9,故可能成立的最小进制是9+1,即10. ...
- MyEclipse 2015 Stable 2.0破解方法
本篇博文简单介绍一下利用网上说明的方法破解MyEclipse 2015 Stable 2.0的具体细节.因为原来在贴吧上的方法不够详细,所以本人重新整理了一下.方法源自:http://tieba.ba ...
- 用 Mathematica 获取图片的 RGB 三基色
ColorConvert[*, "RGB"] // InputForm 其中 * 表示你把你的图片拖入 Mathematica 中.
- [物理学与PDEs]第2章习题13 将 $p$ - 方程组化为守恒律形式的一阶拟线性对称双曲组
试引进新的未知函数, 将 $p$ - 方程组 $$\beex \bea \cfrac{\p \tau}{\p t}-\cfrac{\p u}{\p x}&=0,\\ \cfrac{\p u}{ ...
- 02 Redis关闭服务报错---(error) ERR Errors trying to SHUTDOWN. Check logs.
127.0.0.1:6379> shutdown (error) ERR Errors trying to SHUTDOWN. Check logs. 1.在redis.conf中修改日志文件的 ...