/** * 自动对焦 * * @param cb * @return */ public boolean autoFocus(AutoFocusCallback cb) { if (camera != null) { try { camera.cancelAutoFocus(); if (mParameters != null) { String mode = getAutoFocusMode(); if (StringUtils.isNotEmpty(mode)) { mParameters.setFocusMode(mode); camera.setParameters(mParameters); } } camera.autoFocus(cb); return true; } catch (Exception e) { if (mOnErrorListener != null) { mOnErrorListener.onVideoError(MEDIA_ERROR_CAMERA_AUTO_FOCUS, 0); } if (e != null) Log.e("Yixia", "autoFocus", e); } } return false; }
public CameraPreview(Context context, Camera camera, PreviewCallback previewCb, AutoFocusCallback autoFocusCb) { super(context); this.context=context; mCamera = camera; previewCallback = previewCb; autoFocusCallback = autoFocusCb; /* * Set camera to continuous focus if supported, otherwise use * software auto-focus. Only works for API level >=9. */ /* Camera.Parameters parameters = camera.getParameters(); for (String f : parameters.getSupportedFocusModes()) { if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) { mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); autoFocusCallback = null; break; } } */ // 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); }
@Override public void surfaceChanged(final SurfaceHolder holder, int format, int width, int height) { if (mycamera != null) { mycamera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { if (success) { synchronized (camera) { new Thread() { public void run() { initCamera(holder); super.run(); } }.start(); } } } }); } }
/** * Setups preview button to take a picture state. */ private void setPreviewForPicture() { mPreview.setSoundEffectsEnabled(false); mPreview.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mCamera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean arg0, Camera arg1) { //TODO Is there anything we have to do after autofocus? } }); } }); }
/** * Resets camera parameters and starts camera preview. */ private void resetCam() { try { Camera.Parameters parameters = mCamera.getParameters(); logi("Set Flash mode ON"); parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); mCamera.setParameters(parameters); mCamera.startPreview(); mCamera.autoFocus(new AutoFocusCallback() { public void onAutoFocus(boolean success, Camera camera) { //TODO: add implementation or leave a comment if it's nothing to do here } }); parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); mCamera.setParameters(parameters); } catch(RuntimeException e) { Log.e(TAG, e.toString()); } }
/** * 手动对焦 * * @param focusAreas 对焦区域 * @return */ @SuppressLint("NewApi") @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public boolean manualFocus(AutoFocusCallback cb, List<Area> focusAreas) { if (camera != null && focusAreas != null && mParameters != null && DeviceUtils.hasICS()) { try { camera.cancelAutoFocus(); // getMaxNumFocusAreas检测设备是否支持 if (mParameters.getMaxNumFocusAreas() > 0) { // mParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);// // Macro(close-up) focus mode mParameters.setFocusAreas(focusAreas); } if (mParameters.getMaxNumMeteringAreas() > 0) mParameters.setMeteringAreas(focusAreas); mParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO); camera.setParameters(mParameters); camera.autoFocus(cb); return true; } catch (Exception e) { if (mOnErrorListener != null) { mOnErrorListener.onVideoError(MEDIA_ERROR_CAMERA_AUTO_FOCUS, 0); } if (e != null) Log.e("Yixia", "autoFocus", e); } } return false; }
public void takeAction(final int nAction) { if (CameraPreview.msnFocusMode != 0) { // if not support autofocus mcameraPreview.mbTakeSnapshot = true; mcameraPreview.mnActionAfterTakingSnapshot = nAction; Toast.makeText(ActivityQuickRecog.this, ActivityQuickRecog.this.getString(R.string.camera_preview_clicked), Toast.LENGTH_SHORT).show(); } else if (mCamera != null) { //mCamera.cancelAutoFocus(); // cancel existing auto focus. comment this line because cancel autofcus may hang try { mCamera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { mcameraPreview.mbTakeSnapshot = true; mcameraPreview.mnActionAfterTakingSnapshot = nAction; Toast.makeText(ActivityQuickRecog.this, ActivityQuickRecog.this.getString(R.string.camera_preview_clicked_and_autofocused), Toast.LENGTH_SHORT).show(); } }); } catch(Exception e) { Toast.makeText(ActivityQuickRecog.this, ActivityQuickRecog.this.getString(R.string.camera_preview_fail_to_autofocus), Toast.LENGTH_SHORT).show(); } } }
@Override public boolean onTouch(View v, MotionEvent event) { if (mMediaRecorder == null || !mCreated) { return false; } switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //检测是否手动对焦 showFocusImage(event); if (!mMediaRecorder.onTouch(event, new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { mFocusImage.setVisibility(View.GONE); } })) { return true; } else { mFocusImage.setVisibility(View.GONE); } mMediaRecorder.setAutoFocus(); break; } return true; }
@SuppressWarnings("deprecation") public CameraPreview(Context context, Camera camera, PreviewCallback previewCb, AutoFocusCallback autoFocusCb) { super(context); mCamera = camera; previewCallback = previewCb; autoFocusCallback = autoFocusCb; /* * Set camera to continuous focus if supported, otherwise use * software auto-focus. Only works for API level >=9. */ //API9以上实现连续对焦 // Camera.Parameters parameters = camera.getParameters(); // for (String f : parameters.getSupportedFocusModes()) { // if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) { // parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); // autoFocusCallback = null; // break; // } // } // 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); }
private void initSurfView() { surfaceView = (SurfaceView) findViewById(R.id.surface_view); mSurfaceHolder = surfaceView.getHolder(); mSurfaceHolder.addCallback(this); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mAutoFocusCallback = new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { if (success) { Log.i(TAG, "�Խ��ɹ�"); } else { } } }; }
public void takePicture() { mCamera.setPreviewCallback(null); mCamera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { if(success) { Log.d(TAG, "Focusing...successful."); camera.takePicture(null, null, null, mCall); } else { Log.d(TAG, "Focusing...failed."); camera.takePicture(null, null, null, mCall); } } }); }
public BarcodePreview(Context context, Camera camera, PreviewCallback previewCb, AutoFocusCallback autoFocusCb) { super(context); mCamera = camera; previewCallback = previewCb; autoFocusCallback = autoFocusCb; Camera.Parameters parameters = camera.getParameters(); for (String f : parameters.getSupportedFocusModes()) { if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) { parameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); autoFocusCallback = null; break; } } mHolder = getHolder(); mHolder.addCallback(this); }
public XCameraPreview(Context context, Camera camera, PreviewCallback previewCb, AutoFocusCallback autoFocusCb) { super(context); mCamera = camera; mPreviewCallback = previewCb; mAutoFocusCallback = autoFocusCb; // 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); }
/** * 手动聚焦 * @param point 触屏坐标 */ protected void onFocus(Point point,AutoFocusCallback callback){ Parameters parameters=mCamera.getParameters(); //不支持设置自定义聚焦,则使用自动聚焦,返回 if (parameters.getMaxNumFocusAreas()<=0) { mCamera.autoFocus(callback); return; } List<Area> areas=new ArrayList<Area>(); int left=point.x-300; int top=point.y-300; int right=point.x+300; int bottom=point.y+300; left=left<-1000?-1000:left; top=top<-1000?-1000:top; right=right>1000?1000:right; bottom=bottom>1000?1000:bottom; areas.add(new Area(new Rect(left,top,right,bottom), 100)); parameters.setFocusAreas(areas); try { //本人使用的小米手机在设置聚焦区域的时候经常会出异常,看日志发现是框架层的字符串转int的时候出错了, //目测是小米修改了框架层代码导致,在此try掉,对实际聚焦效果没影响 mCamera.setParameters(parameters); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } mCamera.autoFocus(callback); }
@SuppressWarnings("deprecation") public CameraPreview(Context context, Camera camera, PreviewCallback previewCb, AutoFocusCallback autoFocusCb) { super(context); mCamera = camera; previewCallback = previewCb; autoFocusCallback = autoFocusCb; /* * Set camera to continuous focus if supported, otherwise use software * auto-focus. Only works for API level >=9. */ Camera.Parameters parameters = camera.getParameters(); for (String f: parameters.getSupportedFocusModes()) { if (f == Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) { parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); autoFocusCallback = null; break; } } // 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 CameraPreview(Context context, Camera camera, PreviewCallback previewCb, AutoFocusCallback autoFocusCb) { super(context); mCamera = camera; previewCallback = previewCb; autoFocusCallback = autoFocusCb; /* * Set camera to continuous focus if supported, otherwise use * software auto-focus. Only works for API level >=9. */ /* Camera.Parameters parameters = camera.getParameters(); for (String f : parameters.getSupportedFocusModes()) { if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) { mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); autoFocusCallback = null; break; } } */ // 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 updateCameraParams(){ if(mCamera==null) return; SettingsData dta = InTimeStorage.getInstance().getSettingsData(); Camera.Parameters params = mCamera.getParameters(); //white balance params.setWhiteBalance(dta.getSelectedWhiteBalanceName()); //focus if(dta.getSelectedFocusModeName().equals("auto")){ if(!params.getFocusMode().equals("auto")){ try { mCamera.autoFocus(new AutoFocusCallback() { public void onAutoFocus(boolean success, Camera camera) {} }); } catch (Exception e) { } } }else{ if(params.getFocusMode().equals("auto")){ mCamera.cancelAutoFocus(); } params.setFocusMode(dta.getSelectedFocusModeName()); } //color effect params.setColorEffect(dta.getSelectedColorEfxName()); //flash mode String flashMode = dta.getSelectedFlashModeName(); if(flashMode.equals("on") || flashMode.equals("torch")){ params.setFlashMode("torch"); }else{ params.setFlashMode(flashMode); } mCamera.setParameters(params); }
public CameraPreview(Context context, Camera camera, PreviewCallback previewCb, AutoFocusCallback autoFocusCb) { super(context); mCamera = camera; previewCallback = previewCb; autoFocusCallback = autoFocusCb; // Install a SurfaceHolder.Callback so we get notified when // the underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallback(this); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(checkCameraHardware(this)){ // ��ȡ����ͷʵ�� mCamera = getCameraInstance(); // ����Ԥ������Ķ��� mPreview = new CameraPreview(this, mCamera); FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); //��Ԥ���������Ϊ֡���ֵ��ӽڵ� preview.addView(mPreview); //��ȡ���հ�ť Button captureButton = (Button) findViewById(R.id.button_capture); //���õ������ captureButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { //�Զ��Խ� mCamera.autoFocus(new AutoFocusCallback(){ //�Խ����ʱ���� @Override public void onAutoFocus(boolean success, Camera camera) { //���� mCamera.takePicture(null, null, mPicture); } }); } } ); } }
CameraPreview(Context context, PreviewCallback previewCallback, AutoFocusCallback autoFocusCb) { super(context); mPreviewCallback = previewCallback; mAutoFocusCallback = autoFocusCb; mSurfaceView = new SurfaceView(context); addView(mSurfaceView); // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. mHolder = mSurfaceView.getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); }
@Override public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // initCamera(); mCamera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { if (success) { initCamera();// ʵ������IJ�����ʼ�� camera.cancelAutoFocus();// ֻ�м�������һ�䣬�Ż��Զ��Խ��� } } }); }
/** * Mimic continuous autofocus. * * @return */ private AutoFocusCallback createAutoFocusCallback() { final Runnable doAutoFocus = new Runnable() { public void run() { if (mPreviewing) mCamera.autoFocus(mAutoFocusCB); } }; AutoFocusCallback autoFocusCallback = new Camera.AutoFocusCallback() { public void onAutoFocus(boolean success, Camera camera) { autoFocusHandler.postDelayed(doAutoFocus, 2000); } }; return autoFocusCallback; }
@SuppressWarnings("deprecation") public CameraPreview(Context context, Camera camera, PreviewCallback previewCb, AutoFocusCallback autoFocusCb) { super(context); mCamera = camera; previewCallback = previewCb; autoFocusCallback = autoFocusCb; /* * Set camera to continuous focus if supported, otherwise use software * auto-focus. Only works for API level >=9. */ /* * Camera.Parameters parameters = camera.getParameters(); for (String f * : parameters.getSupportedFocusModes()) { if (f == * Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) { * mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); * autoFocusCallback = null; break; } } */ // 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); }
@Override public void onPrepared() { if (mMediaRecorder != null) { //自动对焦 mMediaRecorder.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { if (success) { } } }); } }
public CameraPreview(Context context, Camera camera, PreviewCallback previewCb, AutoFocusCallback autoFocusCb) { super(context); mCamera = camera; previewCallback = previewCb; autoFocusCallback = autoFocusCb; /* * Set camera to continuous focus if supported, otherwise use * software auto-focus. Only works for API level >=9. */ /* Camera.Parameters parameters = camera.getParameters(); for (String f : parameters.getSupportedFocusModes()) { if (f == Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) { mCamera.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); autoFocusCallback = null; break; } } */ // 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); }
@Override public void onAutoFocus(boolean success, Camera camera) { isAutoFocusing=false; if (getHost() instanceof AutoFocusCallback) { getHost().onAutoFocus(success, camera); } }
@SuppressWarnings("deprecation") private void initCamera(SurfaceHolder holder, int width, int height) { try { if (mCamera == null) { mCamera = Camera.open(0); } holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); Camera.Parameters parameters = mCamera.getParameters(); if (flashModeString != null) { parameters.setFlashMode(flashModeString); } if (parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_AUTO)) { parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); } // flashModeString = Camera.Parameters.FLASH_MODE_OFF; parameters.setPictureFormat(ImageFormat.JPEG); int jpegQuality = CameraProfile.getJpegEncodingQualityParameter(0, CameraProfile.QUALITY_MEDIUM); parameters.setJpegQuality(jpegQuality); List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes(); // Camera.Size preSize = getOptimalPicSize(previewSizes); Camera.Size preSize = getOptimalPicSize(previewSizes); if (preSize != null) { parameters.setPreviewSize(preSize.width, preSize.height); } List<Camera.Size> picSizes = parameters.getSupportedPictureSizes(); Camera.Size picSize = getOptimalPicSize(picSizes); if (picSize != null) { parameters.setPictureSize(picSize.width, picSize.height); } mCamera.setParameters(parameters); mCamera.setDisplayOrientation(getPreviewDegree(getBackOpActivity())); mCamera.setPreviewDisplay(holder); mCamera.startPreview(); mCamera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { isFocusing = false; } }); isFocusing = true; } catch (Exception e) { if (mCamera != null) { mCamera.release(); mCamera = null; } e.printStackTrace(); } }
public void autoFocus(Camera.AutoFocusCallback callback) { if (inPreview) { camera.autoFocus(callback); isAutoFocusing = true; } }
@SuppressLint("NewApi") public boolean onKeyDown(int keyCode) { if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { // zoom in if (mCameraMaxZoom != -1 && mCameraParams.getZoom() != mCameraMaxZoom) { mCamera.startSmoothZoom(mCameraParams.getZoom() + 1); } return true; } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { // zoom out if (mCameraMaxZoom != -1 && mCameraParams.getZoom() != 0) { mCamera.startSmoothZoom(mCameraParams.getZoom() - 1); } return true; } else if (keyCode == KeyEvent.KEYCODE_FOCUS) { if (mCameraAutoFocusEnabled && focusState == UNFOCUSED && !mIsRecording) { focusState = FOCUSING; mCamera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { if (success) { focusState = FOCUS_FINISHED; } else { focusState = UNFOCUSED; } } }); } } else if (keyCode == KeyEvent.KEYCODE_CAMERA && camLoaded) { if (!isVideo()) { if (mCameraAutoFocusEnabled && focusState == FOCUSING) { mCamera.cancelAutoFocus(); focusState = UNFOCUSED; } if (!takePicture()) { StatMethods.hotBread(this.getActivity(), "Camera not loaded! Try again later", Toast.LENGTH_SHORT); } } else { if (mIsRecording) { mPieView.stopAnimation(false); } else { if (prepareVideoRecorder()) { mRecorder.start(); mIsRecording = true; mPieView.setVisibility(View.VISIBLE); mPieView.setAnimationOnCompletionListener(new IOnAnimationCompletion() { @Override public void onAnimationCompletion(View animatedView) { animatedView.setVisibility(View.INVISIBLE); mRecorder.stop(); releaseMediaRecorder(); mIsRecording = false; postVideoCallback(filePath); mVideoToggleButton.setEnabled(true); } }); mPieView.startAnimation(); mVideoToggleButton.setEnabled(false); } else { releaseMediaRecorder(); } } } return true; } return false; }