Refresh the preview in autofocus or continuous autofocus

All your problems with Audio, Video and Images.

Refresh the preview in autofocus or continuous autofocus

Postby el Salmón » Thu Jul 08, 2010 3:37 pm

Hi at all,

I would like to implement an image preview Activity that launch the autofocus when the user press some button and refresh the image on the screen. I don't need take a picture, just show the image preview in almost real-time because I just want to use the camera zoom to read text on the screen. Then I would like to implement a continuous autofocus (every 1 sec. or similar).

I'm basing on the code of this blog. Here it's my interesting code from my Image Preview Activity:
Code: Select all
@Override
   public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

      if (mPreviewRunning) {
         mCamera.stopPreview();
      }

      Camera.Parameters p = mCamera.getParameters();

      // //////////////////////////////////////////////////////////
      // Check orientation and set size as w, h or h, w.
      Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
            .getDefaultDisplay();

      if (display.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
         p.setPreviewSize(h, w);
      } else {
         p.setPreviewSize(w, h);
      }
       p.setAntibanding(Camera.Parameters.ANTIBANDING_AUTO);
      p.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
      setZoom(p);
      mCamera.setParameters(p);

      try {
         mCamera.setPreviewDisplay(holder);
      } catch (IOException e) {
         e.printStackTrace();
      }

      //start autofocus if it was-not started
      AutoFocusCallBackImpl autoFocusCallBack = new AutoFocusCallBackImpl();
      mCamera.autoFocus(autoFocusCallBack);
      //set the bIsAutoFocusStarted trigger to false to avoid extra autofocus attempts
      bIsAutoFocusStarted = true;

      mCamera.startPreview();
      mPreviewRunning = true;
   }


private class AutoFocusCallBackImpl implements Camera.AutoFocusCallback {
      @Override
      public void onAutoFocus(boolean success, Camera camera) {
         bIsAutoFocused = success; // update the flag used in onKeyDown()
         Log.i(TAG, "Inside autofocus callback. autofocused=" + success);
         // play the autofocus sound
         //MediaPlayer.create(Lupe.this, R.raw.auto_focos).start();
      }
   }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
      switch(keyCode){
      case KeyEvent.KEYCODE_DPAD_CENTER:
      case KeyEvent.KEYCODE_CAMERA:
      case KeyEvent.KEYCODE_DPAD_LEFT:
      case KeyEvent.KEYCODE_DPAD_RIGHT:
      case KeyEvent.KEYCODE_DPAD_DOWN:
      case KeyEvent.KEYCODE_DPAD_UP:
      case KeyEvent.KEYCODE_MENU:
         Log.i(TAG, "Autofocus assigned button pressed by the user");
         if (!bIsAutoFocusStarted) {
            Log.i(TAG, "Autofocus started");
            // start autofocus if it was-not started
            AutoFocusCallBackImpl autoFocusCallBack = new AutoFocusCallBackImpl();
            mCamera.autoFocus(autoFocusCallBack);
            // set the bIsAutoFocusStarted trigger to false to avoid extra
            // autofocus attempts
            bIsAutoFocusStarted = true;
            return true;
         }

      case KeyEvent.KEYCODE_BACK:
       //support for the BACK button to exit from activity
          return super.onKeyDown(keyCode, event);
      }
      
        return true;
   }


How to refresh the preview image when onAutoFocus is called?
el Salmón
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jun 22, 2010 3:02 pm
Location: Around 2.4 GHz

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 8 guests