Java Code Examples for android.hardware.camera2.CameraDevice

The following are top voted examples for showing how to use android.hardware.camera2.CameraDevice. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: BWS-Android   File: CameraHelper.java   View source code 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 2
Project: xbot_head   File: CommentaryFragment.java   View source code 6 votes vote down vote up
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();
    }
}
 
Example 3
Project: EvilsLive   File: CameraNew.java   View source code 6 votes vote down vote up
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();
        }
    }
 
Example 4
Project: VideoCRE   File: Camera2Session.java   View source code 6 votes vote down vote up
@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;
  }
}
 
Example 5
Project: VideoCRE   File: Camera2Session.java   View source code 6 votes vote down vote up
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;
  }
}
 
Example 6
Project: AndroidRTC   File: Camera2CapturerTest.java   View source code 5 votes vote down vote up
@Override
public void onClosed(CameraDevice cameraDevice) {
  Logging.d(TAG, "Simple camera2 closed.");

  synchronized (cameraDeviceLock) {
    SimpleCamera2.this.cameraDevice = null;
  }
}
 
Example 7
Project: face-landmark-android   File: ARMaskFragment.java   View source code 5 votes vote down vote up
@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();
}
 
Example 8
Project: opencv-documentscanner-android   File: Camera2BasicFragment.java   View source code 5 votes vote down vote up
@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();
}
 
Example 9
Project: opencv-documentscanner-android   File: Camera2BasicFragment.java   View source code 5 votes vote down vote up
@Override
public void onError(@NonNull CameraDevice cameraDevice, int error) {
    mCameraOpenCloseLock.release();
    cameraDevice.close();
    mCameraDevice = null;
    Activity activity = getActivity();
    if (null != activity) {
        activity.finish();
    }
}
 
Example 10
Project: android-camera2-secret-picture-taker   File: PictureCapturingServiceImpl.java   View source code 5 votes vote down vote up
@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);
    }
}
 
Example 11
Project: android-things-drawbot   File: CameraHandler.java   View source code 5 votes vote down vote up
/**
 * 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");
    }
}
 
Example 12
Project: AI_Calorie_Counter_Demo   File: CameraConnectionFragment.java   View source code 5 votes vote down vote up
@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();
}
 
Example 13
Project: Camera2Vision   File: Camera2Source.java   View source code 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 14
Project: CameraFragment   File: Camera2Manager.java   View source code 5 votes vote down vote up
@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);
            }
        });
    }
}
 
Example 15
Project: CameraFragment   File: Camera2Manager.java   View source code 5 votes vote down vote up
@Override
public void onDisconnected(CameraDevice cameraDevice) {
    cameraDevice.close();
    Camera2Manager.this.cameraDevice = null;

    uiHandler.post(new Runnable() {
        @Override
        public void run() {
            cameraOpenListener.onCameraOpenError();
        }
    });
}
 
Example 16
Project: CameraFragment   File: Camera2Manager.java   View source code 5 votes vote down vote up
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");
    }
}
 
Example 17
Project: LongImageCamera   File: Camera2.java   View source code 5 votes vote down vote up
/**
 * <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");
    }
}
 
Example 18
Project: EZFilter   File: Camera2Input.java   View source code 5 votes vote down vote up
@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();
    }
}
 
Example 19
Project: Android-Code-Demos   File: Camera2BasicFragment.java   View source code 5 votes vote down vote up
@Override
public void onError(@NonNull CameraDevice cameraDevice, int error) {
    mCameraOpenCloseLock.release();
    cameraDevice.close();
    mCameraDevice = null;
    Activity activity = getActivity();
    if (null != activity) {
        activity.finish();
    }
}
 
Example 20
Project: rtmp-rtsp-stream-client-java   File: Camera2ApiManager.java   View source code 5 votes vote down vote up
@Override
public void onOpened(@NonNull CameraDevice cameraDevice) {

  this.cameraDevice = cameraDevice;
  startPreview(cameraDevice);
  Log.i(TAG, "camera opened");
}
 
Example 21
Project: SortingHatAndroid   File: CameraConnectionFragment.java   View source code 5 votes vote down vote up
@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();
}
 
Example 22
Project: ImageClassify   File: CameraConnectionFragment.java   View source code 5 votes vote down vote up
@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();
  }
}
 
Example 23
Project: 361Camera   File: Camera2Fragment.java   View source code 5 votes vote down vote up
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;
    }
}
 
Example 24
Project: FloatingNew   File: CameraPreviewService.java   View source code 5 votes vote down vote up
@Override
public void onOpened(CameraDevice camera) {
    //This is called when the camera is open
    Log.e(TAG, "onOpened");
    cameraDevice = camera;
    createCameraPreview();
}
 
Example 25
Project: Camera_Calibration_Android   File: Camera2BasicFragment.java   View source code 5 votes vote down vote up
@Override
public void onError(@NonNull CameraDevice cameraDevice, int error) {
    mCameraOpenCloseLock.release();
    cameraDevice.close();
    mCameraDevice = null;
    Activity activity = getActivity();
    if (null != activity) {
        activity.finish();
    }
}
 
Example 26
Project: JinsMemeBRIDGE-Android   File: Camera2BasicFragment.java   View source code 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 27
Project: AppRTC-Android   File: Camera2Session.java   View source code 5 votes vote down vote up
@Override
public void onClosed(CameraDevice camera) {
  checkIsOnCameraThread();

  Logging.d(TAG, "Camera device closed.");
  events.onCameraClosed(Camera2Session.this);
}
 
Example 28
Project: fast_face_android   File: CameraConnectionFragment.java   View source code 5 votes vote down vote up
@Override
public void onDisconnected(final CameraDevice cd) {
    cameraOpenCloseLock.release();
    cd.close();
    cameraDevice = null;

    if (mOnGetPreviewListener != null) {
        mOnGetPreviewListener.deInitialize();
    }
}
 
Example 29
Project: fast_face_android   File: CameraConnectionFragment.java   View source code 5 votes vote down vote up
@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();
    }
}
 
Example 30
Project: Cam2Caption   File: Camera2BasicFragment.java   View source code 5 votes vote down vote up
@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();
}
 
Example 31
Project: Android-Camera2Video   File: Camera2VideoFragment.java   View source code 5 votes vote down vote up
@Override
public void onOpened(@NonNull CameraDevice cameraDevice) {
    mCameraDevice = cameraDevice;
    startPreview();
    mCameraOpenCloseLock.release();
    if (null != mTextureView) {
        configureTransform(mTextureView.getWidth(), mTextureView.getHeight());
    }
}
 
Example 32
Project: Android-Camera2Video   File: Camera2VideoFragment.java   View source code 5 votes vote down vote up
@Override
public void onError(@NonNull CameraDevice cameraDevice, int error) {
    mCameraOpenCloseLock.release();
    cameraDevice.close();
    mCameraDevice = null;
    Activity activity = getActivity();
    if (null != activity) {
        activity.finish();
    }
}
 
Example 33
Project: hella-renderscript   File: CameraOps.java   View source code 5 votes vote down vote up
/**
 * 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);
}
 
Example 34
Project: rtmp-rtsp-stream-client-java   File: Camera2ApiManager.java   View source code 5 votes vote down vote up
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;
  }
}
 
Example 35
Project: MOAAP   File: Camera2Renderer.java   View source code 4 votes vote down vote up
@Override
public void onDisconnected(CameraDevice cameraDevice) {
    cameraDevice.close();
    mCameraDevice = null;
    mCameraOpenCloseLock.release();
}
 
Example 36
Project: MOAAP   File: Camera2Renderer.java   View source code 4 votes vote down vote up
@Override
public void onOpened(CameraDevice cameraDevice) {
    mCameraDevice = cameraDevice;
    mCameraOpenCloseLock.release();
    createCameraPreviewSession();
}
 
Example 37
Project: Ocr-android   File: CameraPreview.java   View source code 4 votes vote down vote up
@Override
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
    mCameraOpenCloseLock.release();
    cameraDevice.close();
    mCameraDevice = null;
}
 
Example 38
Project: FloatingNew   File: AndroidCameraApi.java   View source code 4 votes vote down vote up
@Override
public void onError(CameraDevice camera, int error) {
    cameraDevice.close();
    cameraDevice = null;
}
 
Example 39
Project: CameraKitView   File: Camera2Fragment.java   View source code 4 votes vote down vote up
@Override
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
    mCameraOpenCloseLock.release();
    cameraDevice.close();
    mCameraDevice = null;
}
 
Example 40
Project: fingerblox   File: Camera2Renderer.java   View source code 4 votes vote down vote up
@Override
public void onOpened(CameraDevice cameraDevice) {
    mCameraDevice = cameraDevice;
    mCameraOpenCloseLock.release();
    createCameraPreviewSession();
}