欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android CameraManager类详解

程序员文章站 2023-12-15 19:34:28
本文实例为大家分享了android cameramanager类的具体代码,供大家参考,具体内容如下 先看代码: private surfaceview m...

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

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

上一篇:

下一篇: