Android CameraManager类详解

Android CameraManager类详解,第1张

概述本文实例为大家分享了AndroidCameraManager类的具体代码,供大家参考,具体内容如下

本文实例为大家分享了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;    }  }

附:CameraManager类:

package com.lemoncome.facedetection;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 java.io.IOException;import java.util.List;/** * 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 Context mContext;  private boolean initialized;  private boolean prevIEwing;  /**   * 打开摄像头   *   * @param cameraID 摄像头ID   * @return Camera   */  public Camera open(int cameraID) {    int numCameras = Camera.getNumberOfCameras();    if (numCameras == 0) {      Log.e(TAG,"No cameras!");      return null;    }    boolean explicitRequest = cameraID >= 0;    if (!explicitRequest) {      // Select a camera if no explicit camera requested      int index = 0;      while (index < numCameras) {        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();        Camera.getCameraInfo(index,cameraInfo);        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {          break;        }        index++;      }      cameraID = index;    }    Camera camera;    if (cameraID < numCameras) {      Log.e(TAG,"opening camera #" + cameraID);      camera = Camera.open(cameraID);    } else {      if (explicitRequest) {        Log.e(TAG,"Requested camera does not exist: " + cameraID);        camera = null;      } else {        Log.e(TAG,"No camera facing back; returning camera #0");        camera = Camera.open(0);      }    }    int rotation = getdisplayOrIEntation();    camera.setdisplayOrIEntation(rotation);    camera.setPrevIEwCallback(this);    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) {    mContext = context;  }  /**   * 打开camera   *   * @param holder SurfaceHolder   * @throws IOException IOException   */  public synchronized voID openDriver(SurfaceHolder holder)      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 <= 100 & size.wIDth >= w) {          w = size.wIDth;          h = size.height;          break;        }      }      parameters.setPrevIEwSize(w,h);      parameters.setPictureFormat(ImageFormat.JPEG);      parameters.setJpegQuality(100);      parameters.setPictureSize(800,600);      theCamera.setParameters(parameters);    }  }  /**   * 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 voID setPrevIEwCallback(Camera.PrevIEwCallback cb) {    camera.setoneshotPrevIEwCallback(this);  }  /**   * 打开闪光灯   */  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);  }  @OverrIDe  public voID onPrevIEwFrame(byte[] bytes,Camera camera) {  }  private Bitmap reSize(byte[] data) {    Log.i(TAG,"myJpegCallback:onPictureTaken...");    Bitmap cutMap = BitmapFactory.decodeByteArray(data,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,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;  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android CameraManager类详解全部内容,希望文章能够帮你解决Android CameraManager类详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144099.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存