@UiThread private void setFocusMode(Parameters params) { List<String> focusModes = params.getSupportedFocusModes(); if (LOG.isLoggable(INFO)) LOG.info("Focus modes: " + focusModes); if (focusModes.contains(FOCUS_MODE_CONTINUOUS_PICTURE)) { params.setFocusMode(FOCUS_MODE_CONTINUOUS_PICTURE); } else if (focusModes.contains(FOCUS_MODE_CONTINUOUS_VIDEO)) { params.setFocusMode(FOCUS_MODE_CONTINUOUS_VIDEO); } else if (focusModes.contains(FOCUS_MODE_EDOF)) { params.setFocusMode(FOCUS_MODE_EDOF); } else if (focusModes.contains(FOCUS_MODE_MACRO)) { params.setFocusMode(FOCUS_MODE_MACRO); } else if (focusModes.contains(FOCUS_MODE_AUTO)) { params.setFocusMode(FOCUS_MODE_AUTO); } else if (focusModes.contains(FOCUS_MODE_FIXED)) { params.setFocusMode(FOCUS_MODE_FIXED); } }
/** * 通过设置Camera关闭闪光灯 * * @param mCamera */ public void turnLightOff() { if (camera == null) { return; } Parameters parameters = camera.getParameters(); if (parameters == null) { return; } List<String> flashModes = parameters.getSupportedFlashModes(); String flashMode = parameters.getFlashMode(); // Check if camera flash exists if (flashModes == null) { return; } // 闪光灯打开状态 if (!Parameters.FLASH_MODE_OFF.equals(flashMode)) { // Turn off the flash if (flashModes.contains(Parameters.FLASH_MODE_OFF)) { parameters.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(parameters); } else { Log.e(TAG, "FLASH_MODE_OFF not supported"); } } }
public BarcodeCameraConfig configPreviewSize2(int viewWidth, int viewHeight) { Parameters params = camera.getParameters(); List<Size> sizes = params.getSupportedPreviewSizes(); if (sizes == null || sizes.size() <= 0) { return this; } Size bestSize = null; int diff = Integer.MAX_VALUE; for (Size tmpSize : sizes) { int newDiff = Math.abs(tmpSize.width - viewWidth) + Math.abs(tmpSize.height - viewHeight); if (newDiff == 0) { bestSize = tmpSize; break; } else if (newDiff < diff) { bestSize = tmpSize; diff = newDiff; } } params.setPreviewSize(bestSize.width, bestSize.height); camera.setParameters(params); return this; }
@Override public void onOrientationChanged(int orientation) { if (camera.isPresent() && orientation != ORIENTATION_UNKNOWN) { int newOutputOrientation = getCameraPictureRotation(orientation); if (newOutputOrientation != outputOrientation) { outputOrientation = newOutputOrientation; Camera.Parameters params = camera.get().getParameters(); params.setRotation(outputOrientation); try { camera.get().setParameters(params); } catch (Exception e) { Log.e(TAG, "Exception updating camera parameters in orientation change", e); } } } }
@UiThread private void surfaceChangedUi(SurfaceHolder holder, int w, int h) { if (LOG.isLoggable(INFO)) LOG.info("Surface changed: " + w + "x" + h); if (surface != null && surface != holder.getSurface()) { LOG.info("Releasing old surface"); surface.release(); } surface = holder.getSurface(); surfaceWidth = w; surfaceHeight = h; if (camera == null) return; // We are stopped stopPreview(); try { Parameters params = camera.getParameters(); setPreviewSize(params); camera.setParameters(params); logCameraParameters(); } catch (RuntimeException e) { LOG.log(WARNING, "Error setting preview size", e); } startPreview(holder); }
public void setUpSurfaceTexture(final Camera camera) { runOnDraw(new Runnable() { @Override public void run() { try { Camera.Parameters param = camera.getParameters(); param.setPreviewSize(1280, 720);//TODO if (param.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { param.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); } camera.setParameters(param); int[] textures = new int[1]; GLES20.glGenTextures(1, textures, 0); mSurfaceTexture = new SurfaceTexture(textures[0]); camera.setPreviewTexture(mSurfaceTexture); camera.setPreviewCallback(CameraRenderer.this); camera.startPreview(); } catch (IOException e) { e.printStackTrace(); } } }); }
@Override public void onSurfaceChanged(GL10 gl, int width, int height) { GLES20.glViewport(0, 0, width, height); try { mCamera.setPreviewTexture(mSurfaceTexture); Camera.Parameters param = mCamera.getParameters(); param.setPreviewSize(1920, 1080);//TODO if (param.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { param.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); } mCamera.setParameters(param); mCamera.startPreview(); } catch (IOException e) { e.printStackTrace(); } }
@SuppressWarnings("deprecation") public CameraPreview(Context context, Camera camera) { super(context); mCamera = camera; Parameters parameters = mCamera.getParameters(); parameters.setRotation(90); mCamera.setParameters(parameters); // mCamera.setParameters()) // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallback(this); // deprecated setting, but required on Android versions prior to 3.0 mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); }
public void setFlash(boolean flag) { if (this.mCamera != null && CameraUtils.isFlashSupported(this.mCamera)) { Parameters parameters = this.mCamera.getParameters(); if (flag) { if (!parameters.getFlashMode().equals("torch")) { parameters.setFlashMode("torch"); } else { return; } } else if (!parameters.getFlashMode().equals("off")) { parameters.setFlashMode("off"); } else { return; } this.mCamera.setParameters(parameters); } }
@SuppressLint("NewApi") private boolean initCamera() { try { if (frontCamera == 0) { mCamera = Camera.open(CameraInfo.CAMERA_FACING_BACK); } else { mCamera = Camera.open(CameraInfo.CAMERA_FACING_FRONT); } Parameters camParams = mCamera.getParameters(); mCamera.lock(); mSurfaceHolder = mVideoView.getHolder(); mSurfaceHolder.addCallback(this); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mCamera.setDisplayOrientation(90); } catch (RuntimeException ex) { EMLog.e("video", "init Camera fail " + ex.getMessage()); return false; } return true; }
public BarcodeCameraConfig configFocusMode() { try { Parameters parameters = camera.getParameters(); String focusMode = findValue(parameters.getSupportedFocusModes(), Parameters.FOCUS_MODE_AUTO, Parameters.FOCUS_MODE_MACRO); if (focusMode != null) { parameters.setFocusMode(focusMode); } camera.setParameters(parameters); } catch (Exception e) { } return this; }
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { try { if (mCamera != null) { // Now that the size is known, set up the camera parameters and begin // the preview. Camera.Parameters p = mCamera.getParameters(); p.setPreviewSize(mPreviewSize.width, mPreviewSize.height); requestLayout(); setCameraParams(mCamera, p); startPreview(); } } catch (Exception exception) { Log.e(TAG, "Exception caused by surfaceChanged()", exception); } }
public void startPreview() { if (mCamera != null) { // set flash mode if (msbSupportFlash) { Camera.Parameters p = mCamera.getParameters(); if (((ActivityQuickRecog)getContext()).isTurnOnFlashChecked()) { p.setFlashMode(Parameters.FLASH_MODE_TORCH); } else { p.setFlashMode(Parameters.FLASH_MODE_OFF); } setCameraParams(mCamera, p); } // set call back. mCamera.setPreviewCallback(this); // start preview. mCamera.startPreview(); } ((ActivityQuickRecog)getContext()).onStartPreview(); }
/** Turns the LED on or off if phone has one. */ public synchronized void setFlashState(boolean state) { Log.d("flash", "flash called"); // If the camera has already been opened, we apply the change immediately if (mCamera != null) { Log.d("flash", "camera not null"); if (mStreaming && mMode == MODE_MEDIARECORDER_API) { lockCamera(); } Parameters parameters = mCamera.getParameters(); // We test if the phone has a flash if (parameters.getFlashMode()==null) { // The phone has no flash or the choosen camera can not toggle the flash throw new RuntimeException("Can't turn the flash on !"); } else { parameters.setFlashMode(state?Parameters.FLASH_MODE_TORCH:Parameters.FLASH_MODE_OFF); try { mCamera.setParameters(parameters); mFlashEnabled = state; } catch (RuntimeException e) { mFlashEnabled = false; throw new RuntimeException("Can't turn the flash on !"); } finally { if (mStreaming && mMode == MODE_MEDIARECORDER_API) { unlockCamera(); } } } } else { mFlashEnabled = state; } }
@TargetApi(VERSION_CODES.HONEYCOMB) protected List<Size> getSupportedVideoSizes(int currentSdkInt) { Parameters params = mNativeCamera.getNativeCameraParameters(); List<Size> supportedVideoSizes; if (currentSdkInt < Build.VERSION_CODES.HONEYCOMB) { CLog.e(CLog.CAMERA, "Using supportedPreviewSizes iso supportedVideoSizes due to API restriction"); supportedVideoSizes = params.getSupportedPreviewSizes(); } else if (params.getSupportedVideoSizes() == null) { CLog.e(CLog.CAMERA, "Using supportedPreviewSizes because supportedVideoSizes is null"); supportedVideoSizes = params.getSupportedPreviewSizes(); } else { supportedVideoSizes = params.getSupportedVideoSizes(); } return supportedVideoSizes; }
private void setUpCamera(final int id) { mCameraInstance = getCameraInstance(id); Parameters parameters = mCameraInstance.getParameters(); // TODO adjust by getting supportedPreviewSizes and then choosing // the best one for screen size (best fill screen) if (parameters.getSupportedFocusModes().contains( Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); } mCameraInstance.setParameters(parameters); int orientation = mCameraHelper.getCameraDisplayOrientation( ActivityCamera.this, mCurrentCameraId); CameraInfo2 cameraInfo = new CameraInfo2(); mCameraHelper.getCameraInfo(mCurrentCameraId, cameraInfo); boolean flipHorizontal = cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT; mGPUImage.setUpCamera(mCameraInstance, orientation, flipHorizontal, false); }
/** * 设置闪光灯类型 * @param flashMode */ @Override public void setFlashMode(FlashMode flashMode) { if(mCamera==null) return; mFlashMode = flashMode; Parameters parameters=mCamera.getParameters(); switch (flashMode) { case ON: parameters.setFlashMode(Parameters.FLASH_MODE_ON); break; case AUTO: parameters.setFlashMode(Parameters.FLASH_MODE_AUTO); break; case TORCH: parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); break; default: parameters.setFlashMode(Parameters.FLASH_MODE_OFF); break; } mCamera.setParameters(parameters); }
/** * 设置相机缩放级别 * @param zoom */ @Override public void setZoom(int zoom){ if(mCamera==null) return; Parameters parameters; //注意此处为录像模式下的setZoom方式。在Camera.unlock之后,调用getParameters方法会引起android框架底层的异常 //stackoverflow上看到的解释是由于多线程同时访问Camera导致的冲突,所以在此使用录像前保存的mParameters。 if(mParameters!=null) parameters=mParameters; else { parameters=mCamera.getParameters(); } if(!parameters.isZoomSupported()) return; parameters.setZoom(zoom); mCamera.setParameters(parameters); mZoom=zoom; }
/** * 根据当前朝向修改保存图片的旋转角度 */ private void updateCameraOrientation(){ if(mCamera!=null){ Parameters parameters = mCamera.getParameters(); //rotation参数为 0、90、180、270。水平方向为0。 int rotation=90+mOrientation==360?0:90+mOrientation; //前置摄像头需要对垂直方向做变换,否则照片是颠倒的 if(mIsFrontCamera){ if(rotation==90) rotation=270; else if (rotation==270) rotation=90; } parameters.setRotation(rotation);//生成的图片转90° //预览图片旋转90° mCamera.setDisplayOrientation(90);//预览转90° mCamera.setParameters(parameters); } }
public static void setFlashLightNormal(boolean active) { if (camera == null) camera = getCamera(); Parameters params = camera.getParameters(); if (active) { params.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(params); camera.startPreview(); // 开始亮灯; } else { params.setFlashMode(Parameters.FLASH_MODE_OFF); camera.stopPreview(); // 关掉亮灯 camera.release(); // 关掉照相机 camera =null; } ZogUtils.printLog(FlashlightManager.class, "setFlashLightNormal active:" + active); }
/** * A safe way to get an instance of the Camera object. */ public Camera getCameraInstance() { Camera c = null; try { c = Camera.open(cameraId); // attempt to get a Camera instance } catch (Exception e) { Log.e(TAG, "Error: Camera not available!"); // Camera is not available (in use or does not exist) } Parameters params = c.getParameters(); params.setFlashMode(Parameters.FLASH_MODE_OFF); params.setPictureFormat(ImageFormat.JPEG); params.setJpegQuality(50); // 1-100, 100 best c.setParameters(params); return c; // returns null if camera is unavailable }
private void startPreview() { if (myCamera != null) { myCamera.stopPreview(); } else { myCamera = getCameraInstance(); } Parameters parameters = myCamera.getParameters(); if (canOpenFlash) { if (camRecordEnabled) { parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); } } else { parameters.setFlashMode(Parameters.FLASH_MODE_OFF); } parameters.setPreviewFormat(ImageFormat.NV21); myCamera.setParameters(parameters); myCamera.setPreviewCallbackWithBuffer(new MyPreview()); myCamera.addCallbackBuffer(mCallbackBuf); myCamera.startPreview(); }
public boolean prepare() { try { f = new SurfaceTexture(0); // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { // f.setDefaultBufferSize(10000, 10000); // } // SurfaceHolder h = new SurfaceView(JCL_ApplicationContext.getContext()).getHolder(); //mCamera.setPreviewDisplay(h); mCamera.setPreviewTexture(f); } catch (IOException e1) { Log.e("JCLCAMERA", e1.getMessage()); return false; } Parameters params = mCamera.getParameters(); params.setPreviewSize(width, height); params.setPictureSize(width, height); params.setJpegQuality(50); params.setFlashMode(Parameters.FLASH_MODE_OFF); params.setPictureFormat(ImageFormat.JPEG); mCamera.setParameters(params); return true; }
private static Object[] getMaxResolutionOnCamera() { Camera mCamera = getCameraInstance(); Camera.Parameters param; param = mCamera.getParameters(); Camera.Size bestSize = null; List<Camera.Size> sizeList = mCamera.getParameters().getSupportedPreviewSizes(); bestSize = sizeList.get(0); for (int i = 1; i < sizeList.size(); i++) { if ((sizeList.get(i).width * sizeList.get(i).height) > (bestSize.width * bestSize.height)) { bestSize = sizeList.get(i); } } List<Integer> supportedPreviewFormats = param.getSupportedPreviewFormats(); Iterator<Integer> supportedPreviewFormatsIterator = supportedPreviewFormats.iterator(); while (supportedPreviewFormatsIterator.hasNext()) { Integer previewFormat = supportedPreviewFormatsIterator.next(); if (previewFormat == ImageFormat.YV12) { param.setPreviewFormat(previewFormat); } } mCamera.release(); return new Object[]{bestSize.width, bestSize.height}; }
protected synchronized void updateCamera() throws RuntimeException { if (mPreviewStarted) { mPreviewStarted = false; mCamera.stopPreview(); } Parameters parameters = mCamera.getParameters(); mQuality = VideoQuality.determineClosestSupportedResolution(parameters, mQuality); int[] max = VideoQuality.determineMaximumSupportedFramerate(parameters); parameters.setPreviewFormat(mCameraImageFormat); parameters.setPreviewSize(mQuality.resX, mQuality.resY); parameters.setPreviewFpsRange(max[0], max[1]); try { mCamera.setParameters(parameters); mCamera.setDisplayOrientation(mOrientation); mCamera.startPreview(); mPreviewStarted = true; } catch (RuntimeException e) { destroyCamera(); throw e; } }
@SuppressLint("NewApi") private boolean initCamera() { try { if (frontCamera == 0) { mCamera = Camera.open(CameraInfo.CAMERA_FACING_BACK); } else { mCamera = Camera.open(CameraInfo.CAMERA_FACING_FRONT); } Camera.Parameters camParams = mCamera.getParameters(); mCamera.lock(); mSurfaceHolder = mVideoView.getHolder(); mSurfaceHolder.addCallback(this); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mCamera.setDisplayOrientation(90); } catch (RuntimeException ex) { EMLog.e("video", "init Camera fail " + ex.getMessage()); return false; } return true; }
public void setupCameraAndStartPreview(SurfaceHolder sf, Size sz, int displayRotation) { stopCameraPreview(); cameraRotationDegree = CameraHelper.setCameraDisplayOrientation(defaultCameraID, camera, displayRotation); chooseCamcorderProfile(sz); // tweak profile profile.fileFormat = MediaRecorder.OutputFormat.THREE_GPP; profile.audioSampleRate = 16000; profile.audioChannels = 1; profile.audioBitRate = 96000; Parameters param = camera.getParameters(); param.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); param.setPreviewSize(profile.videoFrameWidth, profile.videoFrameHeight); if (Build.VERSION.SDK_INT >= 14) param.setRecordingHint(true); camera.setParameters(param); if (setDisplay(sf)) { startCameraPreview(); } }
/** * ͨ������Camera������� * @param mCamera */ public static boolean hasFlashlight(Camera mCamera) { if (mCamera == null) { return false; } Parameters parameters = mCamera.getParameters(); if (parameters == null) { return false; } List<String> flashModes = parameters.getSupportedFlashModes(); // Check if camera flash exists if (flashModes == null) { // Use the screen as a flashlight (next best thing) return false; } else return true; }
public static void turnLightOn(Camera mCamera) { if (mCamera == null) { return; } Parameters parameters = mCamera.getParameters(); if (parameters == null) { return; } List<String> flashModes = parameters.getSupportedFlashModes(); // Check if camera flash exists if (flashModes == null) { // Use the screen as a flashlight (next best thing) return; } String flashMode = parameters.getFlashMode(); Log.i(TAG, "Flash mode: " + flashMode); Log.i(TAG, "Flash modes: " + flashModes); if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) { // Turn on the flash if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) { parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); mCamera.setParameters(parameters); } else { } } }
/** * フラッシュライト消灯. */ public synchronized void turnOffFlashLight() { if (mCamera != null && isFlashLightState()) { Parameters p = mCamera.getParameters(); p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); mCamera.setParameters(p); if (!isShow()) { mCamera.stopPreview(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { try { mCamera.setPreviewTexture(null); } catch (IOException e) { if (BuildConfig.DEBUG) { e.printStackTrace(); } } } mCamera.release(); mParams = null; mCamera = null; } } mFlashLightState = false; mUseFlashLight = false; }
private void openlight() { try { playSounds(R.raw.click);// �����ť������ ca = Camera.open(); // ������� pa = ca.getParameters(); pa.setFlashMode(Parameters.FLASH_MODE_TORCH);// ѡ���ֵ�Ͳģʽ ca.setParameters(pa); ca.autoFocus(new Camera.AutoFocusCallback() { // �Զ��Խ������ӳɹ��� public void onAutoFocus(boolean success, Camera camera) { } }); ca.startPreview(); state = false; b.setBackground(getResources().getDrawable(R.drawable.on)); } catch (Exception e) { } }
public static void setRotationParameter(Parameters parameters, int cameraId, int orientation) { // See android.hardware.Camera.Parameters.setRotation for // documentation. int rotation = 0; if (orientation != OrientationEventListener.ORIENTATION_UNKNOWN) { CameraInfo info = CameraHolder.instance().getCameraInfo()[cameraId]; // CameraInfo info = new CameraInfo(); // Camera.getCameraInfo(cameraId, info); if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { rotation = (info.orientation - orientation + 360) % 360; } else { // back-facing camera rotation = (info.orientation + orientation) % 360; } } parameters.setRotation(rotation); }
/** * Resets per-recording state. This excludes {@link EglStateSaver}, * which should be re-used across recordings made by this CameraEncoder instance. * * @param config the desired parameters for the next recording. */ private void init(@NonNull SessionConfig config) { mEncodedFirstFrame = false; mReadyForFrames = false; mRecording = false; mEosRequested = false; mCurrentCamera = -1; mDesiredCamera = Camera.CameraInfo.CAMERA_FACING_BACK; mCurrentFlash = Parameters.FLASH_MODE_OFF; mDesiredFlash = null; mCurrentFilter = -1; mNewFilter = Filters.FILTER_NONE; mThumbnailRequested = false; mThumbnailRequestedOnFrame = -1; mSessionConfig = config; }
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { Camera.Parameters parameters = mCamera.getParameters(); List<Size> sizes = parameters.getSupportedPreviewSizes(); Size optimalSize = getOptimalPreviewSize(sizes, w, h); parameters.setPreviewSize(640, 480); String s = String.format("surface w: %d h: %d", w, h); mScreenWidth = w; mScreenHeight = h; Log.i("info", s); // 创建用于预览合成的图片 mbmp_previewL = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);// 创建bitmap对象 mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); // mCamera.setParameters(parameters); mCamera.startPreview(); }
public synchronized void close() { try{ if(mCam == null) { return; } mCamParams.setFlashMode(Parameters.FLASH_MODE_OFF); mCam.setParameters(mCamParams); mCam.setPreviewCallback(null); mCam.stopPreview(); mCam.release(); mCam = null; mInstance = null; } catch(Exception e) { Log.d(Utils.APPNAME, "Exception closing flashlight " + e.getMessage()); } }