0

ImageReader我从这里使用了 Google 示例。

该代码使用Camera2API,ImageReader以便查询图像在与预览图像不同的线程中运行。

由于我想以 Android KitKat (API 20) 为目标,因此我需要修改代码以使用较旧的 Camera API,同时保持ImageReader部件不变。

这是设置的原始代码部分onImageAvailableListener

/**
 * THIS IS CALLED WHEN OPENING CAMERA
 * Sets up member variables related to camera.
 *
 * @param width  The width of available size for camera preview
 * @param height The height of available size for camera preview
 */
private void setUpCameraOutputs(int width, int height) {
 Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        for (String cameraId : manager.getCameraIdList()) {
            CameraCharacteristics characteristics
                    = manager.getCameraCharacteristics(cameraId);

            // We don't use a front facing camera in this sample.
            Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
                continue;
            }

            StreamConfigurationMap map = characteristics.get(
                    CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            if (map == null) {
                continue;
            }

            // For still image captures, we use the largest available size.
            Size largest = Collections.max(
                    Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
                    new CompareSizesByArea());
            mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
                    ImageFormat.JPEG, /*maxImages*/2);
            mImageReader.setOnImageAvailableListener(
                    mOnImageAvailableListener, mBackgroundHandler);
   .
   . 
   .
   .
   }

现在我可以使用较旧的 Camera API。但我迷失在将它与ImageReader. 所以我不知道我应该如何设置onImageListener,以便在图像交付后我可以访问它。

这是我的修改:

  @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mTextureView = (AutoFitTextureView) v.findViewById(R.id.texture);
mTextureView.setSurfaceTextureListener(new SurfaceTextureListener() {
  @Override
  public void onSurfaceTextureUpdated(SurfaceTexture surface) {
  }
  @Override
  public void onSurfaceTextureSizeChanged(SurfaceTexture surface,
      int width, int height) {
  }
  @Override
  public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    return true;
  }
  @Override
  public void onSurfaceTextureAvailable(SurfaceTexture surface,
      int width, int height) {
      mCamera = Camera.open();

    try {

      Camera.Parameters parameters = mCamera.getParameters();

      if (getActivity().getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        // parameters.set("orientation", "portrait"); // For
        // Android Version 2.2 and above
        mCamera.setDisplayOrientation(90);
        // For Android Version 2.0 and above
        parameters.setRotation(90);
      }
      mCamera.setParameters(parameters);
      mCamera.setPreviewTexture(surface);

    } catch (IOException exception) {
      mCamera.release();
    }

    mCamera.startPreview();
    setUpCameraOutputs(width, height); 
    tfPreviewListener.initialize(getActivity().getAssets(), scoreView);

  }
});
}

我的问题是我应该如何添加ImageReader上面的代码以使其正常工作?

提前致谢。

4

0 回答 0