简体   繁体   English

android:相机未拍照

[英]android: camera is not taking a picture

The idea is simple: My App should create a camera object, show a preview and take a picture (without pressing any button) and close the camera afterwards. 这个想法很简单:“我的应用”应创建一个相机对象,显示预览并拍照(不按任何按钮),然后关闭相机。 Unfortunately it is not even taking any pictures. 不幸的是,它甚至没有拍照。 (Means: My callback code which should be executed after the picture is taken is never reached.) (意味着:我的回调代码永远不会到达,应该在拍照后执行。)

I'm using a little wrapper class for the android camera api. 我正在为Android相机api使用一个包装器类。 My activity code looks like this: 我的活动代码如下所示:

camera = new SurveillanceCamera(this, layoutForPreview);
camera.start();
camera.takePicture();

The wrapper class: 包装器类:

public SurveillanceCamera(Context context, LinearLayout previewLayout) {
    cameraInstance = Camera.open(findBackFacingCamera());
    pictureCallback = getPictureCallback();
    cameraPreview = new CameraPreview(context, cameraInstance);
    this.previewLayout = previewLayout;
    this.previewLayout.addView(cameraPreview);
    cameraInstance.startPreview();
  }

  public void takePicture() {
    //this code is reached
    cameraInstance.takePicture(null, null, pictureCallback);
  }

  public void start() {
    if (cameraInstance == null) {
      cameraInstance = Camera.open(findBackFacingCamera());
      pictureCallback = getPictureCallback();
      cameraPreview.setCamera(cameraInstance);
      cameraInstance.startPreview();
    }
  }

  private PictureCallback getPictureCallback() {
    PictureCallback picture = new PictureCallback() {
      @Override
      public void onPictureTaken(byte[] data, Camera camera) {
        Log.e(TAG, "Jey picture was taken...");
        // refresh camera to continue preview
        cameraPreview.refreshCamera(cameraInstance);
      }
    };
    return picture;
  }

  private int findBackFacingCamera() {
    int cameraId = -1;
    // Search for the back facing camera
    // get the number of cameras
    int numberOfCameras = Camera.getNumberOfCameras();
    // for every camera check
    for (int i = 0; i < numberOfCameras; i++) {
      CameraInfo info = new CameraInfo();
      Camera.getCameraInfo(i, info);
      if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
        cameraId = i;
        break;
      }
    }
    return cameraId;
  }

There are no exceptions thrown in the code, but the Log.e() in onPictureTaken is not shown. 代码中没有抛出任何异常,但是onPictureTaken中的Log.e()未显示。 It seems that the camera preview is displayed but nothing happens. 似乎显示了摄像机预览,但是什么也没有发生。 Is it possible, that the preview is not fully loaded at the moment when camera.takePicture() is executed? 是否有可能在执行camera.takePicture()时未完全加载预览? Any suggestions? 有什么建议么?

Your suspicion is most likely justified. 您的怀疑很有可能是合理的。 I don't see when setPreviewDisplay() is called. 调用setPreviewDisplay()时看不到。 But this call should complete for takePicture() to succeed. 但是此调用应完成,以使takePicture()成功。

It is impossible to simply call 简单地打电话是不可能的

camera = new SurveillanceCamera(this, layoutForPreview);
camera.start();
camera.takePicture();

because to complete a setPreviewDisplay() call, you need a fully initialized Holder instance, but you only add the surface view in constructor. 因为要完成setPreviewDisplay()调用,您需要一个完全初始化的Holder实例,但是只能在构造函数中添加表面视图。

It would be best to register a SurfaceHolder.Callback and call takePicture() from the surfaceChanged() callback. 最好注册一个SurfaceHolder.Callback并从surfaceChanged()回调中调用takePicture()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM