Camera自动对焦

private MyAutoFocus mAutoFocus = new MyAutoFocus();
//自动对焦方法
    class MyAutoFocus implements AutoFocusCallback{
        @SuppressLint("NewApi")
        public void onAutoFocus(boolean success, Camera camera) {
            if(mCamera.getParameters().getFocusAreas() != null)
                for(Area a: mCamera.getParameters().getFocusAreas())
                    Log.e("wangshuo","bottom is "+a.rect.bottom+"; top is "+a.rect.top+"; left is "+a.rect.left+"; right is "+a.rect.right);
            focusImage.setImageResource(R.drawable.focus_focusing);
            focusImage.setVisibility(View.VISIBLE);
            ScaleAnimation mScaleAnimation = new ScaleAnimation(2.0f, 1.0f, 2.0f, 1.0f, 
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            mScaleAnimation.setFillAfter(false);
            mScaleAnimation.setDuration(500);
            focusImage.startAnimation(mScaleAnimation);
            if(success){

                focusImage.clearAnimation();
                focusImage.setImageResource(R.drawable.focus_focused);
                mHandler.sendEmptyMessageDelayed(CANCEL_FOCUS, 500);
            }
        }
    }


//调用对焦的地方
  @Override
    protected void onResume() {
        super.onResume();
        getCameraToPreview();
        if(cameraId == 0)//cameraId == 0为后置摄像头,前置摄像头无自动对焦功能
            mCamera.autoFocus(mAutoFocus);
        else {
        //focusImage为对焦成功后显示的图片,有动画效果
            focusImage.setVisibility(View.INVISIBLE);

        }
    }
//mSview是SurfaceView
   mSview= (SurfaceView) findViewById(R.id.preView);
        mSview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(cameraId==0){
                     mCamera.autoFocus(mAutoFocus);//点击自动对焦
                }else{
                     mCamera.autoFocus(null);
                }

            }
        });

你可能感兴趣的:(Android,Camera)