private void doSetTorch(Camera.Parameters parameters, boolean newSetting, boolean safeMode) { String flashMode; if (newSetting) { flashMode = findSettableValue( parameters.getSupportedFlashModes(), Camera.Parameters.FLASH_MODE_TORCH, Camera.Parameters.FLASH_MODE_ON); } else { flashMode = findSettableValue(parameters.getSupportedFlashModes(), Camera.Parameters.FLASH_MODE_OFF); } if (flashMode != null) { parameters.setFlashMode(flashMode); } /* * SharedPreferences prefs = * PreferenceManager.getDefaultSharedPreferences(context); if * (!prefs.getBoolean(PreferencesActivity.KEY_DISABLE_EXPOSURE, false)) * { if (!safeMode) { int minExposure = * parameters.getMinExposureCompensation(); int maxExposure = * parameters.getMaxExposureCompensation(); if (minExposure != 0 || * maxExposure != 0) { float step = * parameters.getExposureCompensationStep(); int desiredCompensation; if * (newSetting) { // Light on; set low exposue compensation * desiredCompensation = Math.max((int) (MIN_EXPOSURE_COMPENSATION / * step), minExposure); } else { // Light off; set high compensation * desiredCompensation = Math.min((int) (MAX_EXPOSURE_COMPENSATION / * step), maxExposure); } Log.i(TAG, "Setting exposure compensation to " * + desiredCompensation + " / " + (step * desiredCompensation)); * parameters.setExposureCompensation(desiredCompensation); } else { * Log.i(TAG, "Camera does not support exposure compensation"); } } } */ }
private void doSetTorch(Camera.Parameters parameters, boolean newSetting, boolean safeMode) { String flashMode; if (newSetting) { flashMode = findSettableValue( parameters.getSupportedFlashModes(), Camera.Parameters.FLASH_MODE_TORCH, Camera.Parameters.FLASH_MODE_ON); } else { flashMode = findSettableValue(parameters.getSupportedFlashModes(), Camera.Parameters.FLASH_MODE_OFF); } if (flashMode != null) { parameters.setFlashMode(flashMode); } /* SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); if (!prefs.getBoolean(PreferencesActivity.KEY_DISABLE_EXPOSURE, false)) { if (!safeMode) { ExposureInterface exposure = new ExposureManager().build(); exposure.setExposure(parameters, newSetting); } } */ }
public int getCameraProperty(String key, int memBuffer, int memBufferSize) { if (mCamera == null) { return MAAPI_consts.MA_CAMERA_RES_FAILED; } Camera.Parameters param = getCurrentParameters(); String result; if (key.equals(MAAPI_consts.MA_CAMERA_IMAGE_FORMAT)) { if (rawMode == true) result = MAAPI_consts.MA_CAMERA_IMAGE_RAW; else result = MAAPI_consts.MA_CAMERA_IMAGE_JPEG; } else if (key.equals(MAAPI_consts.MA_CAMERA_FLASH_SUPPORTED)) { if (param.getSupportedFlashModes() != null) { if (param.getSupportedFlashModes().size() == 1) { result = "false"; } else { result = "true"; } } else { result = "false"; } } else if (key.equals(MAAPI_consts.MA_CAMERA_ZOOM_SUPPORTED)) { if (param.isZoomSupported()) result = "true"; else result = "false"; } else result = param.get(key); if (result == null) return MAAPI_consts.MA_CAMERA_RES_INVALID_PROPERTY_VALUE; if (result.length() + 1 > memBufferSize) { Log.e( "Android Runtime", "maCameraGetProperty: Buffer size " + memBufferSize + " too short to hold buffer of size: " + result.length() + 1); return MAAPI_consts.MA_CAMERA_RES_FAILED; } byte[] ba = result.getBytes(); // Write string to MoSync memory. mMoSyncThread.mMemDataSection.position(memBuffer); mMoSyncThread.mMemDataSection.put(ba); mMoSyncThread.mMemDataSection.put((byte) 0); // ByteBuffer slice = mMoSyncThread.getMemorySlice(memBuffer, ba.length+1); // slice.put( ba ); // slice.put( (byte)0 ); return result.length(); }
public static boolean isFlashSupported(Camera camera, Context context) { mPreferences = PreferenceManager.getDefaultSharedPreferences(context); if (camera != null) { Camera.Parameters mParameters = camera.getParameters(); if (mParameters.getFlashMode() == null) { mPrefFlash = mPreferences.edit().putBoolean("mPrefFlash", false).commit(); return false; } List<String> mSupportedFlashModes = mParameters.getSupportedFlashModes(); if (mSupportedFlashModes == null || mSupportedFlashModes.isEmpty() || mSupportedFlashModes.size() == 1 && mSupportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) { mPrefFlash = mPreferences.edit().putBoolean("mPrefFlash", false).commit(); return false; } } else { mPrefFlash = mPreferences.edit().putBoolean("mPrefFlash", false).commit(); return false; } mPrefFlash = mPreferences.edit().putBoolean("mPrefFlash", true).commit(); return true; }
public void setFlashMode(int cameraType, int flashMode) { Camera camera = _cameras.get(cameraType); if (null == camera) { return; } Camera.Parameters parameters = camera.getParameters(); String value = parameters.getFlashMode(); switch (flashMode) { case RCTCameraModule.RCT_CAMERA_FLASH_MODE_AUTO: value = Camera.Parameters.FLASH_MODE_AUTO; break; case RCTCameraModule.RCT_CAMERA_FLASH_MODE_ON: value = Camera.Parameters.FLASH_MODE_ON; break; case RCTCameraModule.RCT_CAMERA_FLASH_MODE_OFF: value = Camera.Parameters.FLASH_MODE_OFF; break; } List<String> flashModes = parameters.getSupportedFlashModes(); if (flashModes != null && flashModes.contains(value)) { parameters.setFlashMode(value); camera.setParameters(parameters); } }
private void doSetTorch(Camera.Parameters parameters, boolean newSetting) { String flashMode; if (newSetting) { flashMode = findSettableValue( parameters.getSupportedFlashModes(), Camera.Parameters.FLASH_MODE_TORCH, Camera.Parameters.FLASH_MODE_ON); } else { flashMode = findSettableValue(parameters.getSupportedFlashModes(), Camera.Parameters.FLASH_MODE_OFF); } if (flashMode != null) { parameters.setFlashMode(flashMode); } exposure.setExposure(parameters, newSetting); }
/** * Test if this device has a Flashlight we can use and put in Torch mode * * @return boolean */ protected boolean isCapable() { boolean result = false; List<String> flashModes = mParameters.getSupportedFlashModes(); if (flashModes != null && flashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)) { result = true; } return result; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); boolean useFrontCamera = getArguments().getBoolean(FRONT_CAMERA, false); camera = getCameraInstance(useFrontCamera); if (camera == null) { return; } initScreenParams(); parameters = camera.getParameters(); zoomRatios = parameters.getZoomRatios(); zoomIndex = minZoomIndex = 0; maxZoomIndex = parameters.getMaxZoom(); previewSizes = buildPreviewSizesRatioMap(parameters.getSupportedPreviewSizes()); pictureSizes = buildPictureSizesRatioMap(parameters.getSupportedPictureSizes()); List<String> sceneModes = parameters.getSupportedSceneModes(); if (sceneModes != null) { for (String mode : sceneModes) { if (mode.equals(Camera.Parameters.SCENE_MODE_HDR)) { supportedHDR = true; break; } } } // it returns false positive /*getActivity().getApplicationContext().getPackageManager() .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);*/ List<String> flashModes = parameters.getSupportedFlashModes(); if (flashModes == null || flashModes.size() <= 1) { /* Device has no flash */ supportedFlash = false; } else { supportedFlash = true; } if (CameraConst.DEBUG) { Timber.d("PictureSizesRatioMap:"); for (Ratio r : pictureSizes.keySet()) { Timber.d(r.toString() + ":"); for (Quality q : pictureSizes.get(r).keySet()) { Camera.Size size = pictureSizes.get(r).get(q); if (size != null) { Timber.d(q.toString() + ": " + size.width + "x" + size.height); } } } } expandParams(getArguments()); initParams(); }
public static boolean hasFlash() { Camera.Parameters params = getCamera().getParameters(); List<String> flashModes = params.getSupportedFlashModes(); if (flashModes == null) { return false; } for (String flashMode : flashModes) { if (Camera.Parameters.FLASH_MODE_ON.equals(flashMode)) { return true; } } return false; }
/** * Sets the flash mode. * * @param mode flash mode. * @return {@code true} if the flash mode is set, {@code false} otherwise * @see #getFlashMode() */ public boolean setFlashMode(@FlashMode String mode) { synchronized (mCameraLock) { if (mCamera != null && mode != null) { Camera.Parameters parameters = mCamera.getParameters(); if (parameters.getSupportedFlashModes().contains(mode)) { parameters.setFlashMode(mode); mCamera.setParameters(parameters); mFlashMode = mode; return true; } } return false; } }
public int setCameraProperty(String key, String value) { if (mCamera == null) { return MAAPI_consts.MA_CAMERA_RES_FAILED; } try { Camera.Parameters param = getCurrentParameters(); if (key.equals(MAAPI_consts.MA_CAMERA_IMAGE_FORMAT)) { if (value.equals(MAAPI_consts.MA_CAMERA_IMAGE_RAW)) { rawMode = true; } else { // default mode is jpeg rawMode = false; } } else if (key.equals(MAAPI_consts.MA_CAMERA_FOCUS_MODE)) { if (value.equals(MAAPI_consts.MA_CAMERA_FOCUS_AUTO)) { if (false == param.getSupportedFocusModes().contains(value)) { return MAAPI_consts.MA_CAMERA_RES_VALUE_NOTSUPPORTED; } mCamera.autoFocus(autoFocusCallback); } else if (value.equals(MAAPI_consts.MA_CAMERA_FOCUS_MACRO)) { if (false == param.getSupportedFocusModes().contains(value)) { return MAAPI_consts.MA_CAMERA_RES_VALUE_NOTSUPPORTED; } mCamera.autoFocus(autoFocusCallback); } else if (false == param.getSupportedFocusModes().contains(value)) { mCamera.cancelAutoFocus(); return MAAPI_consts.MA_CAMERA_RES_VALUE_NOTSUPPORTED; } else mCamera.cancelAutoFocus(); param.setFocusMode(value); } else if (key.equals(MAAPI_consts.MA_CAMERA_FLASH_MODE)) { if (true == param.getSupportedFlashModes().contains(value)) { param.setFlashMode(value); } else { return MAAPI_consts.MA_CAMERA_RES_VALUE_NOTSUPPORTED; } } else { param.set(key, value); } mCamera.setParameters(param); } catch (Exception e) { return MAAPI_consts.MA_CAMERA_RES_FAILED; } return MAAPI_consts.MA_CAMERA_RES_OK; }
public boolean hasCameraFlash() { if (mCamera == null) { return false; } Camera.Parameters parameters = mCamera.getParameters(); if (parameters.getFlashMode() == null) { return false; } List<String> supportedFlashModes = parameters.getSupportedFlashModes(); if (supportedFlashModes == null || supportedFlashModes.isEmpty() || supportedFlashModes.size() == 1 && supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) { return false; } return true; }
public boolean isFlashSupported(Camera camera) { // Credits: Top answer at http://stackoverflow.com/a/19599365/868173 if (camera != null) { Camera.Parameters parameters = camera.getParameters(); if (parameters.getFlashMode() == null) { return false; } List<String> supportedFlashModes = parameters.getSupportedFlashModes(); if (supportedFlashModes == null || supportedFlashModes.isEmpty() || supportedFlashModes.size() == 1 && supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) { return false; } } else { return false; } return true; }
private static void setTorchEnabled(final Camera camera, final boolean enabled) { final Camera.Parameters parameters = camera.getParameters(); final List<String> supportedFlashModes = parameters.getSupportedFlashModes(); if (supportedFlashModes != null) { final String flashMode; if (enabled) flashMode = findValue( supportedFlashModes, Camera.Parameters.FLASH_MODE_TORCH, Camera.Parameters.FLASH_MODE_ON); else flashMode = findValue(supportedFlashModes, Camera.Parameters.FLASH_MODE_OFF); if (flashMode != null) { camera.cancelAutoFocus(); // autofocus can cause conflict parameters.setFlashMode(flashMode); camera.setParameters(parameters); } } }
// zoom in and enable flash protected boolean initializeCamera(int width, int height) { boolean ret = super.initializeCamera(width, height); Camera.Parameters params = mCamera.getParameters(); List<String> FocusModes = params.getSupportedFocusModes(); if (FocusModes != null && FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) { params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); } else if (FocusModes != null && FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); } List<String> FlashModes = params.getSupportedFlashModes(); if (FlashModes != null && FlashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)) { params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); } if (params.isZoomSupported()) enableZoomControls(params); mCamera.setParameters(params); return ret; }
@Override protected Boolean doInBackground(Void... args) { if (opened) return true; try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { // Choose the first back-facing camera boolean cameraAvailable = false; final CameraInfo cameraInfo = new CameraInfo(); int cameraId = 0; while (!cameraAvailable && cameraId < Camera.getNumberOfCameras()) { Camera.getCameraInfo(cameraId, cameraInfo); if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) cameraAvailable = true; else cameraId++; } if (!cameraAvailable) return false; // No back-facing camera available... camera = Camera.open(cameraId); cameraOrientation = cameraInfo.orientation; } else { camera = Camera.open(); cameraOrientation = 90; // TODO Is this correct? } if (camera == null) return false; // No camera available... final Camera.Parameters params = camera.getParameters(); // Set the maximum resolution based on the maximum heap size final int maxHeapSize = ((ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE)) .getMemoryClass(); final int maxResolution = Math.min( 5 * 1024 * 1024, maxHeapSize * 1024 * 1024 / 4 / 3); // Each image might take up to 33% of the maximum heap size and 5 // megapixels at most // System.err.println("max heap size: " + maxHeapSize); // System.err.println("max resolution: " + maxResolution); // Choose the biggest possible picture size within the limits and the corresponding preview // sizes with the same ratio pictureSize = null; previewSizes = new ArrayList<Size>(); final List<Size> supportedPictureSizes = params.getSupportedPictureSizes(); final List<Size> supportedPreviewSizes = params.getSupportedPreviewSizes(); Collections.sort(supportedPictureSizes, Collections.reverseOrder(sizeComparator)); for (Size supportedPictureSize : supportedPictureSizes) { for (Size supportedPreviewSize : supportedPreviewSizes) { if (supportedPictureSize.width * supportedPreviewSize.height == supportedPictureSize.height * supportedPreviewSize.width && supportedPictureSize.width * supportedPictureSize.height <= maxResolution) { pictureSize = supportedPictureSize; previewSizes.add(supportedPreviewSize); } } if (pictureSize != null) break; } // System.err.println("Selected resolution: " + pictureSize.width + "x" + // pictureSize.height); Collections.sort(previewSizes, Collections.reverseOrder(sizeComparator)); if (pictureSize == null) { // No picture size ratio and preview size ratio match (shouldn't happen...) releaseCamera(); return false; } // Set parameters final List<String> supportedWhiteBalance = params.getSupportedWhiteBalance(); final List<String> supportedFlashModes = params.getSupportedFlashModes(); final List<String> supportedSceneModes = params.getSupportedSceneModes(); final List<String> supportedFocusModes = params.getSupportedFocusModes(); params.setPictureSize(pictureSize.width, pictureSize.height); params.setPreviewSize(previewSizes.get(0).width, previewSizes.get(0).height); if (supportedWhiteBalance != null && supportedWhiteBalance.contains(Camera.Parameters.WHITE_BALANCE_AUTO)) { params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO); } if (supportedFlashModes != null && supportedFlashModes.contains(Camera.Parameters.FLASH_MODE_AUTO)) { params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO); } if (supportedSceneModes != null && supportedSceneModes.contains(Camera.Parameters.SCENE_MODE_AUTO)) { params.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO); } // TODO Decide the preferred focus mode order if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) { params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); autofocus = true; } else if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); autofocus = true; } else if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) { params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); autofocus = false; } else { autofocus = false; } camera.setParameters(params); } catch (Exception e) { releaseCamera(); return false; // Some error while opening the camera... } opened = true; return true; }
/** * Opens the camera and applies the user settings. * * @throws RuntimeException if the method fails */ @SuppressLint("InlinedApi") private Camera createCamera() { int requestedCameraId = getIdForRequestedCamera(mFacing); if (requestedCameraId == -1) { throw new RuntimeException(mContext.getString(R.string.no_requested_camera)); } Camera camera = Camera.open(requestedCameraId); SizePair sizePair = selectSizePair(camera, mRequestedPreviewWidth, mRequestedPreviewHeight, mContext); if (sizePair == null) { throw new RuntimeException(mContext.getString(R.string.no_suitable_preview_size)); } Size pictureSize = sizePair.pictureSize(); mPreviewSize = sizePair.previewSize(); int[] previewFpsRange = selectPreviewFpsRange(camera, mRequestedFps); if (previewFpsRange == null) { throw new RuntimeException(mContext.getString(R.string.no_suitable_frames_per_second)); } Camera.Parameters parameters = camera.getParameters(); parameters.setPictureSize(pictureSize.getWidth(), pictureSize.getHeight()); parameters.setPreviewSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); parameters.setPreviewFpsRange( previewFpsRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX], previewFpsRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]); parameters.setPreviewFormat(ImageFormat.NV21); setRotation(camera, parameters, requestedCameraId); if (mFocusMode != null) { if (parameters.getSupportedFocusModes().contains(mFocusMode)) { parameters.setFocusMode(mFocusMode); } else { Log.i( TAG, mContext.getString(R.string.camera_focus_mode) + mFocusMode + mContext.getString(R.string.not_supported)); } } // setting mFocusMode to the one set in the params mFocusMode = parameters.getFocusMode(); if (mFlashMode != null) { if (parameters.getSupportedFlashModes().contains(mFlashMode)) { parameters.setFlashMode(mFlashMode); } else { Log.i( TAG, mContext.getString(R.string.flash_mode) + mFlashMode + mContext.getString(R.string.not_supported)); } } // setting mFlashMode to the one set in the params mFlashMode = parameters.getFlashMode(); camera.setParameters(parameters); // Four frame buffers are needed for working with the camera: // // one for the frame that is currently being executed upon in doing detection // one for the next pending frame to process immediately upon completing detection // two for the frames that the camera uses to populate future preview images camera.setPreviewCallbackWithBuffer(new CameraPreviewCallback()); camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize)); camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize)); camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize)); camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize)); return camera; }