亲宝软件园·资讯

展开

Android相机聚焦和显示框 Android自定义相机聚焦和显示框

云卷云舒i 人气:0

先看使用效果,白色圆框,放大后缩写并变淡隐藏

下面是代码

public class CameraFocusView extends AppCompatImageView {
 
 String TAG = getClass().getName();
 //显示的圆宽显示的位置
 public float currnetX = 40;
 public float currentY = 50;
 
 Paint paint;
 //半径大小
 float radis = 100;
 //透明度
 int alpha = 255;
 boolean isAlpha = false;
 private ValueAnimator scaleAnimator,colorAnimator;
 
 public CameraFocusView(Context context) {
 super(context);
 init();
 }
 
 public CameraFocusView(Context context, @Nullable AttributeSet attrs) {
 super(context, attrs);
 init();
 }
 
 public CameraFocusView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 init();
 }
 
 public void init() {
 scaleAnimator = new ValueAnimator();
 scaleAnimator.setFloatValues(80, 70, 90);
 scaleAnimator.setDuration(500);
 scaleAnimator.setInterpolator(new LinearInterpolator());
 //伸缩动画效果
 scaleAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  @Override
  public void onAnimationUpdate(ValueAnimator animation) {
  radis = (float) animation.getAnimatedValue();
  invalidate();
  if (radis == 90){
   isAlpha = true;
   colorAnimator.start();
  }
  }
 });
 
 colorAnimator = new ValueAnimator();
 colorAnimator.setIntValues(255,0);
 colorAnimator.setDuration(3000);
 colorAnimator.setInterpolator(new LinearInterpolator());
 //显示透明度动画效果
 colorAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  @Override
  public void onAnimationUpdate(ValueAnimator animation) {
  alpha = (int) animation.getAnimatedValue();
  invalidate();
  }
 });
 
 //初始化不显示
 setVisibility(GONE);
 }
 
 //开始现在圆框
 public void startAnimator() {
 isAlpha = false;
 if (scaleAnimator != null) {
  Log.e(TAG, "startAnimator: scaleAnimator != null");
  setVisibility(VISIBLE);
  scaleAnimator.start();
 } else {
  Log.e(TAG, "startAnimator: scaleAnimator == null");
 }
 }
 
 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 //画笔设置
 paint = new Paint();
 paint.setStyle(Paint.Style.STROKE);//画笔属性是空心圆
 paint.setStrokeWidth(2);//设置画笔粗细
 // 抗锯齿
 paint.setAntiAlias(true);
 paint.setColor(Color.WHITE);
 paint.setAlpha(255);
 if (isAlpha){
  if (alpha > 0 && alpha < 155){
  paint.setAlpha(155);
  }else if (alpha == 0){
  setVisibility(GONE);
  }
 }
 canvas.drawCircle(currnetX, currentY, radis, paint);
 
 }
}

主界面图

调用方式,点击屏幕就能显示

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, Camera.PreviewCallback{
 String TAG = getClass().getName();
 SurfaceView surfaceview;
 SurfaceHolder surfaceHolder;
 Camera camera;
 AutoFocusCallBackClass autoFocusCallBackClass;
 CameraFocusView cameraFocusView;
 
 SensorControler sensorControler;
 
 int width = 480;
 int height = 800;
 int surfaceViewWidth = 480;
 int surfaceViewHeight = 800;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 ConstraintLayout root = findViewById(R.id.root);
 
 surfaceview = findViewById(R.id.surfaceview);
 surfaceHolder = surfaceview.getHolder();
 
 Display display = getWindowManager().getDefaultDisplay();
 width = display.getWidth();
 height = display.getHeight();
 cameraFocusView = new CameraFocusView(this);
 cameraFocusView.setMinimumWidth(width);
 cameraFocusView.setMinimumHeight(height);
 root.addView(cameraFocusView);
 
 
 
 surfaceview.setOnTouchListener(new View.OnTouchListener() {
  @Override
  public boolean onTouch(View view, MotionEvent motionEvent) {
  float reax = motionEvent.getX();
  float reay = motionEvent.getY();
  focusTouch(reax,reay);
  return false;
  }
 });
 
 surfaceHolder.addCallback(this);
 }
 
 
 @Override
 protected void onStart() {
 super.onStart();
 if (sensorControler != null){
  sensorControler.start();
 }
 }
 
 @Override
 protected void onStop() {
 super.onStop();
 if (sensorControler != null){
  sensorControler.stop();
 }
 }
 
 public void focusTouch(float reac_x, float reac_y){
 int areaX = (int)(reac_x / surfaceview.getWidth() * 2000) - 1000;
 int areaY = (int)(reac_y / surfaceview.getHeight() * 2000) - 1000;
 
 if (camera == null){
  return;
 }
 Camera.Parameters parameters = camera.getParameters();
 if (parameters == null){
  return;
 }
 //创建Rect区域
 Rect focusArea = new Rect();
 focusArea.left = Math.max(areaX -100,-1000);
 focusArea.top = Math.max(areaY - 100,-1000);
 focusArea.right = Math.min(areaX + 100,1000);
 focusArea.bottom = Math.min(areaY + 100,1000);
 
//  Log.e(TAG, "onTouch: left = " + focusArea.left + " top = " + focusArea.top + " right = " + focusArea.right + " bottom = " + focusArea.bottom);
 //创建Camera.Area
 Camera.Area cameraArea = new Camera.Area(focusArea,100);
 List<Camera.Area> mereringAreas = new ArrayList<>();
 List<Camera.Area> focusAreas = new ArrayList<>();
 
 if (parameters.getMaxNumMeteringAreas() > 0){
  mereringAreas.add(cameraArea);
  focusAreas.add(cameraArea);
 }
 parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
 parameters.setFocusAreas(focusAreas);
 parameters.setMeteringAreas(mereringAreas);
 try {
  camera.cancelAutoFocus();
  camera.setParameters(parameters);
 }catch (Exception e){
  e.printStackTrace();
 }
 
 if (cameraFocusView != null) {
  cameraFocusView.currnetX = reac_x;
  cameraFocusView.currentY = reac_y;
  cameraFocusView.startAnimator();
 }
 }
 
 @Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
 if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
  Toast.makeText(this,"ok",Toast.LENGTH_SHORT).show();
  initCamera();
 }else {
  Toast.makeText(this,"no",Toast.LENGTH_SHORT).show();
 }
 }
 
 private void initCamera(){
 camera = Camera.open(0);
 surfaceViewWidth = surfaceview.getWidth();
 surfaceViewHeight = surfaceview.getHeight();
 Log.e(TAG, "surfaceCreated: " + surfaceview.getWidth() + " " + surfaceview.getHeight() );
 
 if (camera != null) {
  try {
  camera.setPreviewCallback(this);
  Camera.Parameters parameters = camera.getParameters();
  //需要设置大小,否则在聚焦后就才色调出现缩小
  parameters.setPreviewSize(height,width);
  camera.setParameters(parameters);
  camera.setDisplayOrientation(90);
  camera.setPreviewDisplay(surfaceHolder);
  camera.startPreview();
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
 }
 
 @Override
 public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
 if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
  ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},1);
 }else {
  initCamera();
 }
 }
 
 @Override
 public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
 
 }
 
 @Override
 public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
 if (camera != null){
  camera.setPreviewCallback(null);
  camera.stopPreview();
  camera.release();
  camera = null;
 }
 }
 
 
 @Override
 public void onPreviewFrame(byte[] bytes, Camera camera) {
 
 }
 
 class AutoFocusCallBackClass implements Camera.AutoFocusCallback{
 
 @Override
 public void onAutoFocus(boolean b, Camera camera) {
  Log.e(TAG, "onAutoFocus: " + b);
 }
 }
 
 @Override
 protected void onDestroy() {
 super.onDestroy();
 if (camera != null){
  camera.setPreviewCallback(null);
  camera.stopPreview();
  camera.release();
  camera = null;
 }
}

加载全部内容

相关教程
猜你喜欢
用户评论