Java 类android.hardware.camera2.CameraDevice 实例源码

项目:BWS-Android    文件:CameraHelper.java   
/**
 * does setup the repeating capture request for taking images for the preview
 */
@VisibleForTesting
void setupCaptureRequestForPreview(@NonNull CameraCaptureSession previewSession, @NonNull CameraDevice camera,
                                   @NonNull List<Surface> surfaces) {
    try {
        CaptureRequest.Builder previewRequestBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        for (Surface surface : surfaces) {
            previewRequestBuilder.addTarget(surface);
        }
        previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

        previewSession.setRepeatingRequest(previewRequestBuilder.build(), null, null);

    } catch (CameraAccessException | IllegalStateException e) {
        throw new CameraException(e);
    }
}
项目:xbot_head    文件:CommentaryFragment.java   
private void startPreview() {
    try {
        CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
        StreamConfigurationMap configMap = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

        Size previewSize = Util.getPreferredPreviewSize(
                configMap.getOutputSizes(ImageFormat.JPEG),textureView.getWidth(), textureView.getHeight());

        surfaceTexture.setDefaultBufferSize(previewSize.getWidth(),previewSize.getHeight());
        Surface surface = new Surface(surfaceTexture);
        captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        captureBuilder.addTarget(surface);

        cameraDevice.createCaptureSession(Arrays.asList(surface),captureSessionCallback,backgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
项目:EvilsLive    文件:CameraNew.java   
private void createCameraPreviewSession() {
        if (mCamera == null) {
            return;
        }


        SurfaceHolder holder = surfaceView.getHolder();
//        if (config != null) {
//            holder.setFixedSize(640, 480);
//        }
        Surface surface = holder.getSurface();


        List<Surface> surfaceList = Arrays.asList(surface, mImageReader.getSurface());

        try {
            mPreviewBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            mPreviewBuilder.addTarget(surface);

            mCamera.createCaptureSession(surfaceList, sessionCallback, mHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
项目:VideoCRE    文件:Camera2Session.java   
@Override
public void onOpened(CameraDevice camera) {
  checkIsOnCameraThread();

  Logging.d(TAG, "Camera opened.");
  cameraDevice = camera;

  final SurfaceTexture surfaceTexture = surfaceTextureHelper.getSurfaceTexture();
  surfaceTexture.setDefaultBufferSize(captureFormat.width, captureFormat.height);
  surface = new Surface(surfaceTexture);
  List<Surface> surfaces = new ArrayList<Surface>();
  surfaces.add(surface);
  if (mediaRecorderSurface != null) {
    Logging.d(TAG, "Add MediaRecorder surface to capture session.");
    surfaces.add(mediaRecorderSurface);
  }
  try {
    camera.createCaptureSession(surfaces, new CaptureSessionCallback(), cameraThreadHandler);
  } catch (CameraAccessException e) {
    reportError("Failed to create capture session. " + e);
    return;
  }
}
项目:VideoCRE    文件:Camera2Session.java   
private String getErrorDescription(int errorCode) {
  switch (errorCode) {
    case CameraDevice.StateCallback.ERROR_CAMERA_DEVICE:
      return "Camera device has encountered a fatal error.";
    case CameraDevice.StateCallback.ERROR_CAMERA_DISABLED:
      return "Camera device could not be opened due to a device policy.";
    case CameraDevice.StateCallback.ERROR_CAMERA_IN_USE:
      return "Camera device is in use already.";
    case CameraDevice.StateCallback.ERROR_CAMERA_SERVICE:
      return "Camera service has encountered a fatal error.";
    case CameraDevice.StateCallback.ERROR_MAX_CAMERAS_IN_USE:
      return "Camera device could not be opened because"
          + " there are too many other open camera devices.";
    default:
      return "Unknown camera error: " + errorCode;
  }
}
项目:AndroidRTC    文件:Camera2CapturerTest.java   
@Override
public void onClosed(CameraDevice cameraDevice) {
  Logging.d(TAG, "Simple camera2 closed.");

  synchronized (cameraDeviceLock) {
    SimpleCamera2.this.cameraDevice = null;
  }
}
项目:face-landmark-android    文件:ARMaskFragment.java   
@Override
public void onOpened(final CameraDevice cd) {
    // This method is called when the camera is opened.  We start camera preview here.
    cameraOpenCloseLock.release();
    cameraDevice = cd;
    createCameraPreviewSession();
}
项目:opencv-documentscanner-android    文件:Camera2BasicFragment.java   
@Override
public void onOpened(@NonNull CameraDevice cameraDevice) {
    // This method is called when the camera is opened.  We start camera preview here.
    mCameraOpenCloseLock.release();
    mCameraDevice = cameraDevice;
    createCameraPreviewSession();
}
项目:opencv-documentscanner-android    文件:Camera2BasicFragment.java   
@Override
public void onError(@NonNull CameraDevice cameraDevice, int error) {
    mCameraOpenCloseLock.release();
    cameraDevice.close();
    mCameraDevice = null;
    Activity activity = getActivity();
    if (null != activity) {
        activity.finish();
    }
}
项目:android-camera2-secret-picture-taker    文件:PictureCapturingServiceImpl.java   
@Override
public void onClosed(@NonNull CameraDevice camera) {
    cameraClosed = true;
    Log.d(TAG, "camera " + camera.getId() + " closed");
    //once the current camera has been closed, start taking another picture
    if (!cameraIds.isEmpty()) {
        takeAnotherPicture();
    } else {
        capturingListener.onDoneCapturingAllPhotos(picturesTaken);
    }
}
项目:android-things-drawbot    文件:CameraHandler.java   
/**
 * Execute a new capture request within the active session
 */
private void triggerImageCapture() {
    try {
        final CaptureRequest.Builder captureBuilder =
                mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReader.getSurface());
        captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
        Log.d(TAG, "Capture request created.");
        mCaptureSession.capture(captureBuilder.build(), mCaptureCallback, null);
    } catch (CameraAccessException cae) {
        Log.d(TAG, "camera capture exception");
    } catch (IllegalStateException e) {
        Log.d(TAG, "camera freaked out");
    }
}
项目:AI_Calorie_Counter_Demo    文件:CameraConnectionFragment.java   
@Override
public void onOpened(final CameraDevice cd) {
  // This method is called when the camera is opened.  We start camera preview here.
  cameraOpenCloseLock.release();
  cameraDevice = cd;
  createCameraPreviewSession();
}
项目:Camera2Vision    文件:Camera2Source.java   
/**
 * Capture a still picture. This method should be called when we get a response in
 * {@link #mCaptureCallback} from both {@link #lockFocus()}.
 */
private void captureStillPicture() {
    try {
        if (null == mCameraDevice) {
            return;
        }
        if(mShutterCallback != null) {
            mShutterCallback.onShutter();
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReaderStill.getSurface());

        // Use the same AE and AF modes as the preview.
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, mFocusMode);
        if(mFlashSupported) {
            captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, mFlashMode);
        }

        // Orientation
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(mDisplayOrientation));

        CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() {

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
                unlockFocus();
            }
        };

        mCaptureSession.stopRepeating();
        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
项目:CameraFragment    文件:Camera2Manager.java   
@Override
public void onOpened(CameraDevice cameraDevice) {
    Camera2Manager.this.cameraDevice = cameraDevice;
    if (cameraOpenListener != null) {
        uiHandler.post(new Runnable() {
            @Override
            public void run() {
                if (!TextUtils.isEmpty(currentCameraId) && previewSize != null)
                    cameraOpenListener.onCameraOpened(currentCameraId, previewSize, Camera2Manager.this);
            }
        });
    }
}
项目:CameraFragment    文件:Camera2Manager.java   
@Override
public void onDisconnected(CameraDevice cameraDevice) {
    cameraDevice.close();
    Camera2Manager.this.cameraDevice = null;

    uiHandler.post(new Runnable() {
        @Override
        public void run() {
            cameraOpenListener.onCameraOpenError();
        }
    });
}
项目:CameraFragment    文件:Camera2Manager.java   
private void captureStillPicture() {
    try {
        if (null == cameraDevice) {
            return;
        }
        final CaptureRequest.Builder captureBuilder =
                cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(imageReader.getSurface());

        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getPhotoOrientation(configurationProvider.getSensorPosition()));

        CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() {
            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                                           @NonNull CaptureRequest request,
                                           @NonNull TotalCaptureResult result) {
                Log.d(TAG, "onCaptureCompleted: ");
            }
        };

        captureSession.stopRepeating();
        captureSession.capture(captureBuilder.build(), CaptureCallback, null);

    } catch (CameraAccessException e) {
        Log.e(TAG, "Error during capturing picture");
    }
}
项目:LongImageCamera    文件:Camera2.java   
/**
 * <p>Starts a capture session for camera preview.</p>
 * <p>This rewrites {@link #mPreviewRequestBuilder}.</p>
 * <p>The result will be continuously processed in {@link #mSessionCallback}.</p>
 */
void startCaptureSession() {
    if (!isCameraOpened() || !mPreview.isReady() || mImageReader == null) {
        return;
    }
    Size previewSize = chooseOptimalSize();
    mPreview.setBufferSize(previewSize.getWidth(), previewSize.getHeight());
    Surface surface = mPreview.getSurface();
    try {
        mPreviewRequestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);
        mCamera.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
                mSessionCallback, null);
    } catch (CameraAccessException e) {
        throw new RuntimeException("Failed to start camera session");
    }
}
项目:EZFilter    文件:Camera2Input.java   
@Override
public void takePhoto(boolean isFront, int degree, PhotoTakenCallback callback) {
    mIsFront = isFront;
    mDegree = degree;
    mPhotoTakenCallback = callback;

    CaptureRequest.Builder captureRequestBuilder;
    try {
        captureRequestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        // 将ImageReader的surface作为CaptureRequest.Builder的目标
        captureRequestBuilder.addTarget(mImageReader.getSurface());
        // 自动对焦
        captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        // 自动曝光
        captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
        // 设置照片方向
        if (mIsFront) {
            captureRequestBuilder.set(CaptureRequest.JPEG_ORIENTATION, 270);
        } else {
            captureRequestBuilder.set(CaptureRequest.JPEG_ORIENTATION, 90);
        }
        // 拍照
        CaptureRequest captureRequest = captureRequestBuilder.build();
        mCameraCaptureSession.capture(captureRequest, null, mPreviewHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
项目:SIGHT-For-the-Blind    文件:CameraHandler.java   
/**
 * Execute a new capture request within the active session
 */
private void triggerImageCapture() {
    try {
        final CaptureRequest.Builder captureBuilder =
                mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReader.getSurface());
        captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
        Log.d(TAG, "Capture request created.");
        mCaptureSession.capture(captureBuilder.build(), mCaptureCallback, null);
    } catch (CameraAccessException cae) {
        Log.d(TAG, "camera capture exception");
    }
}
项目:Android-Code-Demos    文件:Camera2BasicFragment.java   
@Override
public void onError(@NonNull CameraDevice cameraDevice, int error) {
    mCameraOpenCloseLock.release();
    cameraDevice.close();
    mCameraDevice = null;
    Activity activity = getActivity();
    if (null != activity) {
        activity.finish();
    }
}
项目:rtmp-rtsp-stream-client-java    文件:Camera2ApiManager.java   
@Override
public void onOpened(@NonNull CameraDevice cameraDevice) {

  this.cameraDevice = cameraDevice;
  startPreview(cameraDevice);
  Log.i(TAG, "camera opened");
}
项目:SortingHatAndroid    文件:CameraConnectionFragment.java   
@Override public void onOpened(final CameraDevice cd) {
  // This method is called when the camera is opened.  We start camera preview here.
  cameraOpenCloseLock.release();
  cameraDevice = cd;

  createCameraPreviewSession();
}
项目:ImageClassify    文件:CameraConnectionFragment.java   
@Override
public void onError(final CameraDevice cd, final int error) {
  cameraOpenCloseLock.release();
  cd.close();
  cameraDevice = null;
  final Activity activity = getActivity();
  if (null != activity) {
    activity.finish();
  }
}
项目:361Camera    文件:Camera2Fragment.java   
private boolean sendRepeatPreviewRequest() {
    try {
        CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        builder.addTarget(mPreviewSurface);
        builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
        builder.set(CaptureRequest.CONTROL_AF_MODE, mControlAFMode.switchToCamera2FocusMode());

        mCaptureSession.setRepeatingRequest(builder.build(), mPreCaptureCallback, mBackgroundHandler);
        return true;
    } catch (CameraAccessException e) {
        e.printStackTrace();
        return false;
    }
}
项目:FloatingNew    文件:CameraPreviewService.java   
@Override
public void onOpened(CameraDevice camera) {
    //This is called when the camera is open
    Log.e(TAG, "onOpened");
    cameraDevice = camera;
    createCameraPreview();
}
项目:Camera_Calibration_Android    文件:Camera2BasicFragment.java   
@Override
public void onError(@NonNull CameraDevice cameraDevice, int error) {
    mCameraOpenCloseLock.release();
    cameraDevice.close();
    mCameraDevice = null;
    Activity activity = getActivity();
    if (null != activity) {
        activity.finish();
    }
}
项目:JinsMemeBRIDGE-Android    文件:Camera2BasicFragment.java   
/**
 * Capture a still picture. This method should be called when we get a response in
 * {@link #mCaptureCallback} from both {@link #lockFocus()}.
 */
private void captureStillPicture() {
    try {
        final Activity activity = getActivity();
        if (null == activity || null == mCameraDevice) {
            return;
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final CaptureRequest.Builder captureBuilder =
                mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(mImageReader.getSurface());

        // Use the same AE and AF modes as the preview.
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        setAutoFlash(captureBuilder);

        // Orientation
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));

        CameraCaptureSession.CaptureCallback CaptureCallback
                = new CameraCaptureSession.CaptureCallback() {

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                                           @NonNull CaptureRequest request,
                                           @NonNull TotalCaptureResult result) {
                unlockFocus();
            }
        };

        mCaptureSession.stopRepeating();
        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
项目:AppRTC-Android    文件:Camera2Session.java   
@Override
public void onClosed(CameraDevice camera) {
  checkIsOnCameraThread();

  Logging.d(TAG, "Camera device closed.");
  events.onCameraClosed(Camera2Session.this);
}
项目:fast_face_android    文件:CameraConnectionFragment.java   
@Override
public void onDisconnected(final CameraDevice cd) {
    cameraOpenCloseLock.release();
    cd.close();
    cameraDevice = null;

    if (mOnGetPreviewListener != null) {
        mOnGetPreviewListener.deInitialize();
    }
}
项目:fast_face_android    文件:CameraConnectionFragment.java   
@Override
public void onError(final CameraDevice cd, final int error) {
    cameraOpenCloseLock.release();
    cd.close();
    cameraDevice = null;
    final Activity activity = getActivity();
    if (null != activity) {
        activity.finish();
    }

    if (mOnGetPreviewListener != null) {
        mOnGetPreviewListener.deInitialize();
    }
}
项目:Cam2Caption    文件:Camera2BasicFragment.java   
@Override
public void onOpened(@NonNull CameraDevice cameraDevice) {
    // This method is called when the camera is opened.  We start camera preview here.
    mCameraOpenCloseLock.release();
    mCameraDevice = cameraDevice;
    createCameraPreviewSession();
}
项目:Android-Camera2Video    文件:Camera2VideoFragment.java   
@Override
public void onOpened(@NonNull CameraDevice cameraDevice) {
    mCameraDevice = cameraDevice;
    startPreview();
    mCameraOpenCloseLock.release();
    if (null != mTextureView) {
        configureTransform(mTextureView.getWidth(), mTextureView.getHeight());
    }
}
项目:Android-Camera2Video    文件:Camera2VideoFragment.java   
@Override
public void onError(@NonNull CameraDevice cameraDevice, int error) {
    mCameraOpenCloseLock.release();
    cameraDevice.close();
    mCameraDevice = null;
    Activity activity = getActivity();
    if (null != activity) {
        activity.finish();
    }
}
项目:hella-renderscript    文件:CameraOps.java   
/**
 * Get a request builder for the current camera.
 */
public CaptureRequest.Builder createCaptureRequest(int template) throws CameraAccessException {
    CameraDevice device = mCameraDevice;
    if (device == null) {
        throw new IllegalStateException("Can't get requests when no camera is open");
    }
    return device.createCaptureRequest(template);
}
项目:rtmp-rtsp-stream-client-java    文件:Camera2ApiManager.java   
private CaptureRequest drawPreview(Surface surface) {
  try {
    CaptureRequest.Builder captureRequestBuilder =
        cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
    captureRequestBuilder.addTarget(surface);
    captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
    return captureRequestBuilder.build();
  } catch (CameraAccessException e) {
    e.printStackTrace();
    return null;
  }
}
项目:MOAAP    文件:Camera2Renderer.java   
@Override
public void onDisconnected(CameraDevice cameraDevice) {
    cameraDevice.close();
    mCameraDevice = null;
    mCameraOpenCloseLock.release();
}
项目:MOAAP    文件:Camera2Renderer.java   
@Override
public void onOpened(CameraDevice cameraDevice) {
    mCameraDevice = cameraDevice;
    mCameraOpenCloseLock.release();
    createCameraPreviewSession();
}
项目:Ocr-android    文件:CameraPreview.java   
@Override
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
    mCameraOpenCloseLock.release();
    cameraDevice.close();
    mCameraDevice = null;
}
项目:FloatingNew    文件:AndroidCameraApi.java   
@Override
public void onError(CameraDevice camera, int error) {
    cameraDevice.close();
    cameraDevice = null;
}
项目:CameraKitView    文件:Camera2Fragment.java   
@Override
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
    mCameraOpenCloseLock.release();
    cameraDevice.close();
    mCameraDevice = null;
}