android.hardware.Camera.open()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(292)

本文整理了Java中android.hardware.Camera.open()方法的一些代码示例,展示了Camera.open()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Camera.open()方法的具体详情如下:
包路径:android.hardware.Camera
类名称:Camera
方法名:open

Camera.open介绍

暂无

代码示例

代码示例来源:origin: dm77/barcodescanner

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(int cameraId) {
  Camera c = null;
  try {
    if(cameraId == -1) {
      c = Camera.open(); // attempt to get a Camera instance
    } else {
      c = Camera.open(cameraId); // attempt to get a Camera instance
    }
  }
  catch (Exception e) {
    // Camera is not available (in use or does not exist)
  }
  return c; // returns null if camera is unavailable
}

代码示例来源:origin: vondear/RxTool

/**
 * 打开闪光灯
 *
 * @return
 */
public static void openFlashLight() {
  try {
    if (camera == null) {
      camera = Camera.open();
      camera.startPreview();
    }
    Camera.Parameters parameters = camera.getParameters();
    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    camera.setParameters(parameters);
  } catch (Exception e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: journeyapps/zxing-android-embedded

/**
   * Opens the requested camera with {@link Camera#open(int)}, if one exists.
   *
   * @param requestedId camera ID of the camera to use. A negative value
   *                    or {@link #NO_REQUESTED_CAMERA} means "no preference"
   * @return handle to {@link Camera} that was opened
   */
  public static Camera open(int requestedId) {
    int cameraId = getCameraId(requestedId);
    if (cameraId == -1) {
      return null;
    } else {
      return Camera.open(cameraId);
    }
  }
}

代码示例来源:origin: nekocode/CameraFilter

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
  if (renderThread != null && renderThread.isAlive()) {
    renderThread.interrupt();
  }
  renderThread = new Thread(this);
  surfaceTexture = surface;
  gwidth = -width;
  gheight = -height;
  // Open camera
  Pair<Camera.CameraInfo, Integer> backCamera = getBackCamera();
  final int backCameraId = backCamera.second;
  camera = Camera.open(backCameraId);
  // Start rendering
  renderThread.start();
}

代码示例来源:origin: daniulive/SmarterStreaming

@SuppressLint("NewApi")
private Camera openCamera(int type){
  int frontIndex =-1;
  int backIndex = -1;
  int cameraCount = Camera.getNumberOfCameras();
  Log.i(TAG, "cameraCount: " + cameraCount);
  
  CameraInfo info = new CameraInfo();
  for(int cameraIndex = 0; cameraIndex<cameraCount; cameraIndex++){
    Camera.getCameraInfo(cameraIndex, info);
    
    if(info.facing == CameraInfo.CAMERA_FACING_FRONT){
      frontIndex = cameraIndex;
    }else if(info.facing == CameraInfo.CAMERA_FACING_BACK){
      backIndex = cameraIndex;
    }
  }
  pushCurrentCameraType = type;
  if(type == FRONT && frontIndex != -1){
    pushCurCameraIndex = frontIndex;
    return Camera.open(frontIndex);
  }else if(type == BACK && backIndex != -1){
    pushCurCameraIndex = backIndex;
    return Camera.open(backIndex);
  }
  
  return null;
}

代码示例来源:origin: daniulive/SmarterStreaming

@SuppressLint("NewApi")
private Camera openCamera(int type) {
  int frontIndex = -1;
  int backIndex = -1;
  int cameraCount = Camera.getNumberOfCameras();
  Log.i(TAG, "cameraCount: " + cameraCount);
  CameraInfo info = new CameraInfo();
  for (int cameraIndex = 0; cameraIndex < cameraCount; cameraIndex++) {
    Camera.getCameraInfo(cameraIndex, info);
    if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
      frontIndex = cameraIndex;
    } else if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
      backIndex = cameraIndex;
    }
  }
  currentCameraType = type;
  if (type == FRONT && frontIndex != -1) {
    curCameraIndex = frontIndex;
    return Camera.open(frontIndex);
  } else if (type == BACK && backIndex != -1) {
    curCameraIndex = backIndex;
    return Camera.open(backIndex);
  }
  return null;
}

代码示例来源:origin: daniulive/SmarterStreaming

@SuppressLint("NewApi")
private Camera openCamera(int type){
  int frontIndex =-1;
  int backIndex = -1;
  int cameraCount = Camera.getNumberOfCameras();
  Log.i(TAG, "cameraCount: " + cameraCount);
  CameraInfo info = new CameraInfo();
  for(int cameraIndex = 0; cameraIndex<cameraCount; cameraIndex++){
    Camera.getCameraInfo(cameraIndex, info);
    if(info.facing == CameraInfo.CAMERA_FACING_FRONT){
      frontIndex = cameraIndex;
    }else if(info.facing == CameraInfo.CAMERA_FACING_BACK){
      backIndex = cameraIndex;
    }
  }
  currentCameraType = type;
  if(type == FRONT && frontIndex != -1){
    curCameraIndex = frontIndex;
    return Camera.open(frontIndex);
  }else if(type == BACK && backIndex != -1){
    curCameraIndex = backIndex;
    return Camera.open(backIndex);
  }
  return null;
}

代码示例来源:origin: JZ-Darkal/AndroidHttpCapture

if (cameraId < numCameras) {
  Log.i(TAG, "Opening camera #" + cameraId);
  camera = Camera.open(cameraId);
} else {
  if (explicitRequest) {
  } else {
    Log.i(TAG, "No camera facing back; returning camera #0");
    camera = Camera.open(0);

代码示例来源:origin: TommyLemon/APIJSON

/**
 * Opens the camera driver and initializes the hardware parameters.
 *
 * @param holder The surface object which the camera will draw preview frames into.
 * @throws IOException Indicates the camera driver failed to open.
 */
public void openDriver(SurfaceHolder holder) throws IOException {
  if (camera == null) {
    camera = Camera.open();
    if (camera == null) {
      throw new IOException();
    }
    camera.setPreviewDisplay(holder);
    if (!initialized) {
      initialized = true;
      configManager.initFromCameraParameters(camera);
    }
    configManager.setDesiredCameraParameters(camera);
    //FIXME
    //     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    //�Ƿ�ʹ��ǰ��
    //      if (prefs.getBoolean(PreferencesActivity.KEY_FRONT_LIGHT, false)) {
    //        FlashlightManager.enableFlashlight();
    //      }
    FlashlightManager.enableFlashlight();
  }
}

代码示例来源:origin: rmtheis/android-ocr

/**
 * Opens the camera driver and initializes the hardware parameters.
 *
 * @param holder The surface object which the camera will draw preview frames into.
 * @throws IOException Indicates the camera driver failed to open.
 */
public synchronized void openDriver(SurfaceHolder holder) throws IOException {
 Camera theCamera = camera;
 if (theCamera == null) {
  theCamera = Camera.open();
  if (theCamera == null) {
   throw new IOException();
  }
  camera = theCamera;
 }
 camera.setPreviewDisplay(holder);
 if (!initialized) {
  initialized = true;
  configManager.initFromCameraParameters(theCamera);
  if (requestedFramingRectWidth > 0 && requestedFramingRectHeight > 0) {
   adjustFramingRect(requestedFramingRectWidth, requestedFramingRectHeight);
   requestedFramingRectWidth = 0;
   requestedFramingRectHeight = 0;
  }
 }
 configManager.setDesiredCameraParameters(theCamera);
 
 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
 reverseImage = prefs.getBoolean(PreferencesActivity.KEY_REVERSE_IMAGE, false);
}

代码示例来源:origin: yipianfengye/android-zxingLibrary

/**
   * Opens the camera driver and initializes the hardware parameters.
   *
   * @param holder The surface object which the camera will draw preview frames into.
   * @throws IOException Indicates the camera driver failed to open.
   */
  public void openDriver(SurfaceHolder holder) throws IOException {
    if (camera == null) {
      camera = Camera.open();
      if (camera == null) {
        throw new IOException();
      }
      camera.setPreviewDisplay(holder);

      if (!initialized) {
        initialized = true;
        configManager.initFromCameraParameters(camera);
      }
      configManager.setDesiredCameraParameters(camera);

      //FIXME
      //     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
      //�Ƿ�ʹ��ǰ��
//      if (prefs.getBoolean(PreferencesActivity.KEY_FRONT_LIGHT, false)) {
//        FlashlightManager.enableFlashlight();
//      }
      FlashlightManager.enableFlashlight();
    }
  }

代码示例来源:origin: TommyLemon/Android-ZBLibrary

/**
 * Opens the camera driver and initializes the hardware parameters.
 *
 * @param holder The surface object which the camera will draw preview frames into.
 * @throws IOException Indicates the camera driver failed to open.
 */
public void openDriver(SurfaceHolder holder) throws IOException {
  if (camera == null) {
    camera = Camera.open();
    if (camera == null) {
      throw new IOException();
    }
    camera.setPreviewDisplay(holder);
    if (!initialized) {
      initialized = true;
      configManager.initFromCameraParameters(camera);
    }
    configManager.setDesiredCameraParameters(camera);
    //FIXME
    //     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    //�Ƿ�ʹ��ǰ��
    //      if (prefs.getBoolean(PreferencesActivity.KEY_FRONT_LIGHT, false)) {
    //        FlashlightManager.enableFlashlight();
    //      }
    FlashlightManager.enableFlashlight();
  }
}

代码示例来源:origin: florent37/CameraFragment

@Override
public void run() {
  try {
    camera = Camera.open(cameraId);
    prepareCameraOutputs();
    if(futurFlashMode != null) {

代码示例来源:origin: robolectric/robolectric

@Before
public void setUp() throws Exception {
 camera = Camera.open();
 shadowCamera = Shadows.shadowOf(camera);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testOpenWithId() throws Exception {
 camera = Camera.open(12);
 assertThat(camera).isNotNull();
 assertThat(ShadowCamera.getLastOpenedCameraId()).isEqualTo(12);
}

代码示例来源:origin: LaiFeng-Android/SopCastComponent

mCameraDevice = Camera.open(cameraData.cameraID);
} catch (RuntimeException e) {
  SopCastLog.e(TAG, "fail to connect Camera");

代码示例来源:origin: robolectric/robolectric

@Test
public void testSetDisplayOrientationUpdatesCameraInfos() {
 addBackCamera();
 addFrontCamera();
 camera = Camera.open(1);
 camera.setDisplayOrientation(180);
 Camera.CameraInfo cameraQuery = new Camera.CameraInfo();
 Camera.getCameraInfo(ShadowCamera.getLastOpenedCameraId(), cameraQuery);
 assertThat(cameraQuery.orientation).isEqualTo(180);
}

代码示例来源:origin: vondear/RxTool

private void openCamera() {
  if (mCamera != null) {
    releaseCamera();
  }
  mCamera = Camera.open(mCameraId);
  mCameraParameters = mCamera.getParameters();
  // Supported preview sizes
  mPreviewSizes.clear();
  for (Camera.Size size : mCameraParameters.getSupportedPreviewSizes()) {
    mPreviewSizes.add(new Size(size.width, size.height));
  }
  // Supported picture sizes;
  mPictureSizes.clear();
  for (Camera.Size size : mCameraParameters.getSupportedPictureSizes()) {
    mPictureSizes.add(new Size(size.width, size.height));
  }
  // AspectRatio
  if (mAspectRatio == null) {
    mAspectRatio = Constants.DEFAULT_ASPECT_RATIO;
  }
  adjustCameraParameters();
  mCamera.setDisplayOrientation(calcDisplayOrientation(mDisplayOrientation));
  mCallback.onCameraOpened();
}

代码示例来源:origin: googlesamples/android-vision

throw new RuntimeException("Could not find requested camera.");
Camera camera = Camera.open(requestedCameraId);

代码示例来源:origin: google/cameraview

private void openCamera() {
  if (mCamera != null) {
    releaseCamera();
  }
  mCamera = Camera.open(mCameraId);
  mCameraParameters = mCamera.getParameters();
  // Supported preview sizes
  mPreviewSizes.clear();
  for (Camera.Size size : mCameraParameters.getSupportedPreviewSizes()) {
    mPreviewSizes.add(new Size(size.width, size.height));
  }
  // Supported picture sizes;
  mPictureSizes.clear();
  for (Camera.Size size : mCameraParameters.getSupportedPictureSizes()) {
    mPictureSizes.add(new Size(size.width, size.height));
  }
  // AspectRatio
  if (mAspectRatio == null) {
    mAspectRatio = Constants.DEFAULT_ASPECT_RATIO;
  }
  adjustCameraParameters();
  mCamera.setDisplayOrientation(calcDisplayOrientation(mDisplayOrientation));
  mCallback.onCameraOpened();
}

相关文章