Advertisement
Guest User

Autofocus

a guest
Mar 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. // Start recognition when autofocus completes (used when continuous autofocus is disabled)
  2.     private Camera.AutoFocusCallback startRecognitionCameraAutoFocusCallback = new Camera.AutoFocusCallback() {
  3.         @Override
  4.         public void onAutoFocus( boolean success, Camera camera )
  5.         {
  6.             startRecognition();
  7.         }
  8.     };
  9.  
  10.     // Enable 'Start' button when autofocus completes (used when continuous autofocus is disabled)
  11.     private Camera.AutoFocusCallback enableStartButtonCameraAutoFocusCallback = new Camera.AutoFocusCallback() {
  12.         @Override
  13.         public void onAutoFocus( boolean success, Camera camera )
  14.         {
  15.             startButton.setText( BUTTON_TEXT_START );
  16.             startButton.setEnabled( true );
  17.         }
  18.     };
  19.  
  20.     // Start autofocus (used when continuous autofocus is disabled)
  21.     private void autoFocus( Camera.AutoFocusCallback callback )
  22.     {
  23.         if( camera != null ) {
  24.             try {
  25.                 camera.autoFocus( callback );
  26.             } catch( Exception e ) {
  27.                 Log.e( getString( R.string.app_name ), "Error: " + e.getMessage() );
  28.             }
  29.         }
  30.     }
  31.  
  32.  
  33. // Choosing autofocus or continuous focus and whether to start recognition immediately or after autofocus or manually
  34.         if( disableContinuousAutofocus ||
  35.             !parameters.getSupportedFocusModes().contains( Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO ) ) {
  36.             // No continuous focus
  37.             if( startRecognitionWhenReady ) {
  38.                 // Start recognition (if you look inside onStartButtonClick it is actually delayed
  39.                 // till autofocus completes
  40.                 onStartButtonClick( startButton );
  41.                 startRecognitionWhenReady = false;
  42.             } else {
  43.                 // Just focus and enable 'Start' button
  44.                 autoFocus( enableStartButtonCameraAutoFocusCallback );
  45.             }
  46.         } else {
  47.             // Continuous focus. Have to use some Magic. Some devices expect preview to actually
  48.             // start before enabling continuous focus has any effect. So we wait for the camera to
  49.             // actually start preview
  50.             handler.postDelayed( new Runnable() {
  51.                 public void run()
  52.                 {
  53.                     Camera _camera = MainActivity.this.camera;
  54.                     Camera.Parameters parameters = _camera.getParameters();
  55.                     parameters.setFocusMode( Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO );
  56.                     _camera.setParameters( parameters );
  57.                     if( startRecognitionWhenReady ) {
  58.                         // Give some time for the camera to focus and start recognition
  59.                         handler.postDelayed( new Runnable() {
  60.                             public void run()
  61.                             {
  62.                                 onStartButtonClick( startButton );
  63.                             }
  64.  
  65.                         }, 300 );
  66.                         startRecognitionWhenReady = false;
  67.                     } else {
  68.                         // Just enable 'Start' button
  69.                         startButton.setEnabled( true );
  70.                         startButton.setText( BUTTON_TEXT_START );
  71.                     }
  72.                 }
  73.             }, 300 );
  74.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement