android.hardware.camera2.CameraManager Java Examples

The following examples show how to use android.hardware.camera2.CameraManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Helper.java    From VIA-AI with MIT License 8 votes vote down vote up
public void forceScanAllCameras(Activity activity) {
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        String[] idList = manager.getCameraIdList();

        int maxCameraCnt = idList.length;

        for (int index = 0; index < maxCameraCnt; index++) {
            String cameraId = manager.getCameraIdList()[index];
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
            StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }

}
 
Example #2
Source File: VIACamera.java    From VIA-AI with MIT License 8 votes vote down vote up
@TargetApi(21)
public static Size[] querySupportResolution(Context context, int index) {
    CameraManager manager =
            (CameraManager)context.getSystemService(CAMERA_SERVICE);
    try {
        CameraCharacteristics chars
                = manager.getCameraCharacteristics(index+"");

        StreamConfigurationMap configurationMap = chars.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

        return configurationMap.getOutputSizes(ImageFormat.YUV_420_888);

    } catch(CameraAccessException e){
        e.printStackTrace();
    }
    return null;
}
 
Example #3
Source File: Camera2Controller.java    From pixelvisualcorecamera with Apache License 2.0 6 votes vote down vote up
public Size[] getSupportedPictureSizes(String cameraId) {
  Size[] outputSizes = null;
  CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
  try {
    CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
    StreamConfigurationMap map = characteristics.get(
        CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
    if (map != null) {
      outputSizes = map.getOutputSizes(ImageFormat.JPEG);
    }
  } catch (CameraAccessException e) {
    Log.w(TAG, e);
  }

  return outputSizes;
}
 
Example #4
Source File: CameraXUtil.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@RequiresApi(21)
public static int getLowestSupportedHardwareLevel(@NonNull Context context) {
  @SuppressLint("RestrictedApi") CameraManager cameraManager = CameraManagerCompat.from(context).unwrap();

  try {
    int supported = maxHardwareLevel();

    for (String cameraId : cameraManager.getCameraIdList()) {
      CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
      Integer               hwLevel         = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);

      if (hwLevel == null || hwLevel == CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) {
        return CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY;
      }

      supported = smallerHardwareLevel(supported, hwLevel);
    }

    return supported;
  } catch (CameraAccessException e) {
    Log.w(TAG, "Failed to enumerate cameras", e);

    return CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY;
  }
}
 
Example #5
Source File: Camera2Helper.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
/**
 * カメラID に対応したカメラデバイスがサポートしているプレビューサイズのリストを取得します.
 *
 * @param cameraManager カメラマネージャ
 * @param cameraId カメラID
 * @return サポートしているプレビューサイズのリスト
 */
@NonNull
static List<Size> getSupportedPreviewSizes(final CameraManager cameraManager, final String cameraId) {
    List<Size> previewSizes = new ArrayList<>();
    try {
        CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
        StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        if (map != null) {
            previewSizes = Arrays.asList(map.getOutputSizes(SurfaceTexture.class));
            Collections.sort(previewSizes, SizeComparator);
        }
    } catch (CameraAccessException e) {
        // ignore.
    }
    return previewSizes;
}
 
Example #6
Source File: Camera.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private static boolean isMonochrome(@NonNull String deviceName, @NonNull CameraManager cameraManager) {
  try {
    CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(deviceName);
    int[]                 capabilities    = characteristics.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES);

    if (capabilities != null) {
      for (int capability : capabilities) {
        if (capability == CameraMetadata.REQUEST_AVAILABLE_CAPABILITIES_MONOCHROME) {
          return true;
        }
      }
    }
  } catch (CameraAccessException e) {
    return false;
  }

  return false;
}
 
Example #7
Source File: Camera2Source.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 6 votes vote down vote up
public boolean isCamera2Native() {
    try {
        if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }
        manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
        mCameraId = manager.getCameraIdList()[mFacing];
        CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraId);
        //CHECK CAMERA HARDWARE LEVEL. IF CAMERA2 IS NOT NATIVELY SUPPORTED, GO BACK TO CAMERA1
        Integer deviceLevel = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
        return deviceLevel != null && (deviceLevel != CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY);
    } catch (CameraAccessException ex) {
        return false;
    } catch (NullPointerException e) {
        return false;
    } catch (ArrayIndexOutOfBoundsException ez) {
        return false;
    }
}
 
Example #8
Source File: CustomVideoCapturerCamera2.java    From opentok-android-sdk-samples with MIT License 6 votes vote down vote up
public CustomVideoCapturerCamera2(Context ctx,
                            Publisher.CameraCaptureResolution resolution,
                            Publisher.CameraCaptureFrameRate fps) {
    cameraManager = (CameraManager) ctx.getSystemService(Context.CAMERA_SERVICE);
    display = ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    camera = null;
    cameraState = CameraState.CLOSED;
    reentrantLock = new ReentrantLock();
    condition = reentrantLock.newCondition();
    frameDimensions = resolutionTable.get(resolution.ordinal());
    desiredFps = frameRateTable.get(fps.ordinal());
    runtimeExceptionList = new ArrayList<RuntimeException>();
    isPaused = false;
    try {
        String camId = selectCamera(PREFERRED_FACING_CAMERA);
        /* if default camera facing direction is not found, use first camera */
        if (null == camId && (0 < cameraManager.getCameraIdList().length)) {
            camId = cameraManager.getCameraIdList()[0];
        }
        cameraIndex = findCameraIndex(camId);
    } catch (CameraAccessException e) {
        throw new Camera2Exception(e.getMessage());
    }
}
 
Example #9
Source File: AndroidCamera2AgentImpl.java    From Camera2 with Apache License 2.0 6 votes vote down vote up
AndroidCamera2AgentImpl(Context context) {
    mCameraHandlerThread = new HandlerThread("Camera2 Handler Thread");
    mCameraHandlerThread.start();
    mCameraHandler = new Camera2Handler(mCameraHandlerThread.getLooper());
    mExceptionHandler = new CameraExceptionHandler(mCameraHandler);
    mCameraState = new AndroidCamera2StateHolder();
    mDispatchThread = new DispatchThread(mCameraHandler, mCameraHandlerThread);
    mDispatchThread.start();
    mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
    mNoisemaker = new MediaActionSound();
    mNoisemaker.load(MediaActionSound.SHUTTER_CLICK);

    mNumCameraDevices = 0;
    mCameraDevices = new ArrayList<String>();
    updateCameraDevices();
}
 
Example #10
Source File: Camera2DeviceTester.java    From Camera2 with Apache License 2.0 6 votes vote down vote up
public CameraDevice captureCameraDevice() throws Exception {
    CameraManager manager =
            (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
    String id = manager.getCameraIdList()[0];
    synchronized (this) {
        manager.openCamera(id, this, sHandler);
        wait();
    }
    return mCamera;
}
 
Example #11
Source File: Camera2WrapperManager.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
/**
 * カメラが取り付けられている向きを取得します.
 *
 * @param context コンテキスト
 * @param facing カメラのタイプ
 * @return カメラの向き(0, 90, 180, 270)
 */
public static int getSensorOrientation(Context context, int facing) {
    CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
    if (manager == null) {
        throw new UnsupportedOperationException("Not supported a Camera.");
    }

    try {
        String cameraId = Camera2Helper.getCameraId(manager, facing);
        if (cameraId != null) {
            return Camera2Helper.getSensorOrientation(manager, cameraId);
        }
    } catch (CameraAccessException e) {
        // ignore.
    }

    throw new UnsupportedOperationException("Not supported a Camera.");
}
 
Example #12
Source File: ScanQRActivity.java    From ScreenCapture with MIT License 6 votes vote down vote up
private float getMinimumFocusDistance() {
    if (mCameraId == null)
        return 0;

    Float minimumLens = null;
    try {
        CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        CameraCharacteristics c = manager.getCameraCharacteristics(mCameraId);
        minimumLens = c.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE);
    } catch (Exception e) {
        Log.e(TAG, "isHardwareLevelSupported Error", e);
    }
    if (minimumLens != null)
        return minimumLens;
    return 0;
}
 
Example #13
Source File: CameraUtils.java    From SimpleSmsRemote with MIT License 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private static List<MyCameraInfo> GetAllCameras2(Context context) throws Exception {
    List<MyCameraInfo> cameras = new ArrayList<>();

    CameraManager cameraManager =
            (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);

    for (String cameraId : cameraManager.getCameraIdList()) {
        CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
        MyCameraInfo cameraInfo = MyCameraInfo.CreateFromCameraCharacteristics(cameraId,
                characteristics);
        cameras.add(cameraInfo);
    }

    return cameras;
}
 
Example #14
Source File: VideoCameraActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void initCamera() {
  try {
    CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    String[] cameraIds = cameraManager.getCameraIdList();
    if (cameraIds.length == 0) return;

    String cameraId = cameraIds[0];

    if (ActivityCompat.checkSelfPermission(this,
      Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(this,
        new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSIONS_REQUEST);
    }

    cameraManager.openCamera(cameraId, cameraDeviceCallback, null);
  } catch (CameraAccessException e) {
    Log.d(TAG, "No access to the camera.", e);
  }
}
 
Example #15
Source File: MediaSnippetsActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void initCamera() {
  cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

  String[] cameraIds = new String[0];

  try {
    cameraIds = cameraManager.getCameraIdList();
    if (cameraIds.length == 0) return;

    cameraId = cameraIds[0];
  } catch (CameraAccessException e) {
    Log.e(TAG, "Camera Error.", e);
    return;
  }
}
 
Example #16
Source File: AndroidCamera2AgentImpl.java    From Camera2 with Apache License 2.0 5 votes vote down vote up
public AndroidCamera2DeviceInfo(CameraManager cameraManager,
                                String[] cameraIds, int numberOfCameras) {
    mCameraManager = cameraManager;
    mCameraIds = cameraIds;
    mNumberOfCameras = numberOfCameras;

    int firstBackId = NO_DEVICE;
    int firstFrontId = NO_DEVICE;
    for (int id = 0; id < cameraIds.length; ++id) {
        try {
            int lensDirection = cameraManager.getCameraCharacteristics(cameraIds[id])
                    .get(CameraCharacteristics.LENS_FACING);
            if (firstBackId == NO_DEVICE &&
                    lensDirection == CameraCharacteristics.LENS_FACING_BACK) {
                firstBackId = id;
            }
            if (firstFrontId == NO_DEVICE &&
                    lensDirection == CameraCharacteristics.LENS_FACING_FRONT) {
                firstFrontId = id;
            }
        } catch (CameraAccessException ex) {
            Log.w(TAG, "Couldn't get characteristics of camera '" + id + "'", ex);
        }
    }
    mFirstBackCameraId = firstBackId;
    mFirstFrontCameraId = firstFrontId;
}
 
Example #17
Source File: Camera2Manager.java    From sandriosCamera with MIT License 5 votes vote down vote up
@Override
public void initializeCameraManager(ConfigurationProvider configurationProvider, Context context) {
    super.initializeCameraManager(configurationProvider, context);

    this.manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);

    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    windowSize = new Size(size.x, size.y);

    try {
        String[] ids = manager.getCameraIdList();
        numberOfCameras = ids.length;
        for (String id : ids) {
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(id);

            int orientation = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (orientation == CameraCharacteristics.LENS_FACING_FRONT) {
                faceFrontCameraId = id;
                faceFrontCameraOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
                frontCameraCharacteristics = characteristics;
            } else {
                faceBackCameraId = id;
                faceBackCameraOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
                backCameraCharacteristics = characteristics;
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Error during camera init");
    }
}
 
Example #18
Source File: RemoteCamera.java    From GoFIT_SDK_Android with Apache License 2.0 5 votes vote down vote up
private int getNumberOfCameras() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        try {
            return NUMBER_OF_CAMERA = (((CameraManager) getSystemService(Context.CAMERA_SERVICE)).getCameraIdList()).length;
        } catch (CameraAccessException e) {
            Log.e("CameraAccessException", e.toString());
            return 0;
        }
    } else {
        //noinspection deprecation
        return NUMBER_OF_CAMERA = Camera.getNumberOfCameras();
    }
}
 
Example #19
Source File: Camera2Manager.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public void initializeCameraManager(CameraConfigProvider cameraConfigProvider, Context context) {
    super.initializeCameraManager(cameraConfigProvider, context);
    final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    final Display display = windowManager.getDefaultDisplay();
    final Point size = new Point();
    display.getSize(size);
    mWindowSize = new Size(size.x, size.y);

    mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
    try {
        final String[] ids = mCameraManager.getCameraIdList();
        mNumberOfCameras = ids.length;
        for (String id : ids) {
            final CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(id);

            final int orientation = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (orientation == CameraCharacteristics.LENS_FACING_FRONT) {
                mFaceFrontCameraId = id;
                mFaceFrontCameraOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
                mFrontCameraCharacteristics = characteristics;
            } else {
                mFaceBackCameraId = id;
                mFaceBackCameraOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
                mBackCameraCharacteristics = characteristics;
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Error during camera initialize");
    }
}
 
Example #20
Source File: Camera2OneCameraOpenerImpl.java    From Camera2 with Apache License 2.0 5 votes vote down vote up
public static Optional<OneCameraOpener> create(
        OneCameraFeatureConfig featureConfig,
        Context context,
        ActiveCameraDeviceTracker activeCameraDeviceTracker,
        DisplayMetrics displayMetrics)
{
    if (!ApiHelper.HAS_CAMERA_2_API)
    {
        return Optional.absent();
    }
    CameraManager cameraManager;
    try
    {
        cameraManager = AndroidServices.instance().provideCameraManager();
    } catch (IllegalStateException ex)
    {
        Log.e(TAG, "camera2.CameraManager is not available.");
        return Optional.absent();
    }
    OneCameraOpener oneCameraOpener = new Camera2OneCameraOpenerImpl(
            featureConfig,
            context,
            cameraManager,
            activeCameraDeviceTracker,
            displayMetrics);
    return Optional.of(oneCameraOpener);
}
 
Example #21
Source File: CameraModuleHelper.java    From Camera2 with Apache License 2.0 5 votes vote down vote up
public static Camera2ActionProvider provideCamera2ActionProvider()
{
    CameraManager cameraManager = AndroidServices.instance().provideCameraManager();

    HandlerFactory handlerFactory = new HandlerFactory();
    ExecutorService backgroundRunner = Executors.newSingleThreadExecutor();

    return new Camera2ActionProvider(cameraManager, handlerFactory, backgroundRunner,
            Loggers.tagFactory());
}
 
Example #22
Source File: MainActivity.java    From journaldev with MIT License 5 votes vote down vote up
private void flashLightOn() {
    CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

    try {
        String cameraId = cameraManager.getCameraIdList()[0];
        cameraManager.setTorchMode(cameraId, true);
    } catch (CameraAccessException e) {
    }
}
 
Example #23
Source File: BaseCameraActivity.java    From fritz-examples with MIT License 5 votes vote down vote up
private String chooseCamera() {
    final CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        for (final String cameraId : manager.getCameraIdList()) {
            final CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

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

            final StreamConfigurationMap map =
                    characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

            if (map == null) {
                continue;
            }

            // Fallback to camera1 API for internal cameras that don't have full support.
            // This should help with legacy situations where using the camera2 API causes
            // distorted or otherwise broken previews.
            useCamera2API = (facing == CameraCharacteristics.LENS_FACING_EXTERNAL)
                    || isHardwareLevelSupported(characteristics,
                    CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL);
            Log.i(TAG, "Camera API lv2?: " + useCamera2API);
            return cameraId;
        }
    } catch (CameraAccessException e) {
        Log.e(TAG, "Not allowed to access camera: " + e);
    }

    return null;
}
 
Example #24
Source File: BaseCameraActivity.java    From fritz-examples with MIT License 5 votes vote down vote up
private String chooseCamera() {
    final CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        for (final String cameraId : manager.getCameraIdList()) {
            final CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

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

            final StreamConfigurationMap map =
                    characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

            if (map == null) {
                continue;
            }

            // Fallback to camera1 API for internal cameras that don't have full support.
            // This should help with legacy situations where using the camera2 API causes
            // distorted or otherwise broken previews.
            useCamera2API = (facing == CameraCharacteristics.LENS_FACING_EXTERNAL)
                    || isHardwareLevelSupported(characteristics,
                    CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL);
            Log.i(TAG, "Camera API lv2?: " + useCamera2API);
            return cameraId;
        }
    } catch (CameraAccessException e) {
        Log.e(TAG, "Not allowed to access camera: " + e);
    }

    return null;
}
 
Example #25
Source File: CameraActivity.java    From dbclf with Apache License 2.0 5 votes vote down vote up
protected String chooseCamera() {
    final CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        for (final String cameraId : manager.getCameraIdList()) {
            final CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

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

            final StreamConfigurationMap map =
                    characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

            if (map == null) {
                continue;
            }

            // Fallback to camera1 API for internal cameras that don't have full support.
            // This should help with legacy situations where using the camera2 API causes
            // distorted or otherwise broken previews.
            useCamera2API = (facing == CameraCharacteristics.LENS_FACING_EXTERNAL)
                    || isHardwareLevelSupported(characteristics,
                    CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL);
            return cameraId;
        }
    } catch (CameraAccessException ignored) {
    }

    return null;
}
 
Example #26
Source File: BaseCameraActivity.java    From fritz-examples with MIT License 5 votes vote down vote up
private String chooseCamera() {
    final CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        for (final String cameraId : manager.getCameraIdList()) {
            final CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

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

            final StreamConfigurationMap map =
                    characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

            if (map == null) {
                continue;
            }

            // Fallback to camera1 API for internal cameras that don't have full support.
            // This should help with legacy situations where using the camera2 API causes
            // distorted or otherwise broken previews.
            useCamera2API = (facing == CameraCharacteristics.LENS_FACING_EXTERNAL)
                    || isHardwareLevelSupported(characteristics,
                    CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL);
            Log.i(TAG, "Camera API lv2?: " + useCamera2API);
            return cameraId;
        }
    } catch (CameraAccessException e) {
        Log.e(TAG, "Not allowed to access camera: " + e);
    }

    return null;
}
 
Example #27
Source File: Camera2.java    From TikTok with Apache License 2.0 5 votes vote down vote up
Camera2(Callback callback, PreviewImpl preview, Context context) {
    super(callback, preview);
    mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
    mPreview.setCallback(new PreviewImpl.Callback() {
        @Override
        public void onSurfaceChanged() {
            startCaptureSession();
        }
    });
}
 
Example #28
Source File: BaseCameraActivity.java    From fritz-examples with MIT License 5 votes vote down vote up
private String chooseCamera() {
    final CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        for (final String cameraId : manager.getCameraIdList()) {
            final CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

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

            final StreamConfigurationMap map =
                    characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

            if (map == null) {
                continue;
            }

            // Fallback to camera1 API for internal cameras that don't have full support.
            // This should help with legacy situations where using the camera2 API causes
            // distorted or otherwise broken previews.
            useCamera2API = (facing == CameraCharacteristics.LENS_FACING_EXTERNAL)
                    || isHardwareLevelSupported(characteristics,
                    CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL);
            Log.i(TAG, "Camera API lv2?: " + useCamera2API);
            return cameraId;
        }
    } catch (CameraAccessException e) {
        Log.e(TAG, "Not allowed to access camera: " + e);
    }

    return null;
}
 
Example #29
Source File: CameraConnectionFragment.java    From dbclf with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up member variables related to camera.
 */
private void setUpCameraOutputs() {
    final Activity activity = getActivity();
    final CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        final CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

        final StreamConfigurationMap map =
                characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

        sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);

        // Danger, W.R.! Attempting to use too large a preview size could  exceed the camera
        // bus' bandwidth limitation, resulting in gorgeous previews but the storage of
        // garbage capture data.
        previewSize =
                chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
                        inputSize.getWidth(),
                        inputSize.getHeight());

        // We fit the aspect ratio of TextureView to the size of preview we picked.
        final int orientation = getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            textureView.setAspectRatio(previewSize.getWidth(), previewSize.getHeight());
        } else {
            textureView.setAspectRatio(previewSize.getHeight(), previewSize.getWidth());
        }
    } catch (final CameraAccessException ignored) {
    } catch (final NullPointerException e) {
        // Currently an NPE is thrown when the Camera2API is used but not supported on the
        // device this code runs.
        throw new RuntimeException(getString(R.string.camera_error));
    }

    cameraConnectionCallback.onPreviewSizeChosen(previewSize, sensorOrientation);
}
 
Example #30
Source File: MainActivity.java    From Android-9-Development-Cookbook with MIT License 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mButtonLight = findViewById(R.id.buttonLight);
    mCameraManager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
    mCameraId = getCameraId();
    if (mCameraId==null) {
        mButtonLight.setEnabled(false);
    } else {
        mButtonLight.setEnabled(true);
    }
}