1. Are you ready for the Galaxy S20? Here is everything we know so far!

Apps Loop camera autofocus

Discussion in 'Android Development' started by pietjuhhh1990, Feb 21, 2011.

  1. pietjuhhh1990

    pietjuhhh1990 Well-Known Member
    Thread Starter

    I am still a bit new to android.
    I've used C# and a lot of web based coding languages.

    I'm making an app in witch the autofocus must work. The problem is that the autofocus function is pretty bad if the distance is short. Since there is no manual way of setting this I want to make the autofocus functon loop 5 times to try and get the right focus and quit when the focus is good.

    this are the 2 peaces of code for the camera:
    Code (Text):
    1.  
    2.             success = false;    
    3.             int iLoop = 5;
    4.             //loop autofocus for positive focus
    5.             while (iLoop >= 0 && success == false)
    6.             {
    7.                 iLoop--;
    8.                 mCamera.autoFocus(myAutoFocusCallback);    
    9.                 try {
    10.                     //give camera time 2 focus
    11.                     Thread.sleep(1500);
    12.                 } catch (InterruptedException e) {
    13.                     // TODO Auto-generated catch block
    14.                 }
    15.             }
    16.  
    Code (Text):
    1.  
    2.  
    3.     AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){
    4.    
    5.           public void onAutoFocus(boolean success, Camera mCamera) {
    6.               success = true;
    7.               return;
    8.           }
    9.     };
    10.  
    When the camera is focused is shoud automaticly return true. I've manually changed the value of succes to true in the onAutoFocus in the hope it would work.

    I hope someone could help

    Thanx in advance :)
     

    Advertisement

  2. pietjuhhh1990

    pietjuhhh1990 Well-Known Member
    Thread Starter

    I've made the return Bool show up on screen and used @Override in the hope it would work but still this code wil result with

    5 = false
    4 = false
    3 = false
    2 = false
    1 = false

    the weird thing however is that the camere uses autofocus 5 times in a row but it does'nt write aany lines down until the loop is compleate then al 5 rows will be written

    here's the new piece of code

    Code (Text):
    1.             success = false;
    2.             TextView tvDebug = (TextView) findViewById(R.id.tvDebug);
    3.             tvDebug.setText("");
    4.             int iLoop = 5;
    5.             //loop autofocus for positive focus
    6.             while (iLoop >= 1 && success == false)
    7.             {
    8.                 mCamera.autoFocus(myAutoFocusCallback);    
    9.                 String currText = tvDebug.getText().toString();
    10.                
    11.                 String combine = currText + " \n " + String.valueOf(iLoop) + " = " + String.valueOf(success);
    12.                
    13.                 tvDebug.setText(combine);
    14.                
    15.                 try {
    16.                     Thread.sleep(1500);
    17.                 } catch (InterruptedException e) {
    18.                     // TODO Auto-generated catch block
    19.                 }
    20.  
    21.                 iLoop--;
    22.             }
    23.  
    and the autofocus callback, i added a returnbool but it also din't help

    Code (Text):
    1.  
    2.     AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){
    3.    
    4.         @Override    
    5.           public void onAutoFocus(boolean success, Camera mCamera) {
    6.                   success = true;
    7.               return;
    8.           }
    9.        
    10.         public boolean onAutoFocus(){
    11.             success = true;
    12.             return success;
    13.         }
    14.     };
    15.  

    I hope someone knows what's wrong
     
  3. miXer

    miXer Android Enthusiast

    First, when you know how many times to loop, always use a for loop.
    Code (Text):
    1.  
    2.             success = false;
    3.             TextView tvDebug = (TextView) findViewById(R.id.tvDebug);
    4.             tvDebug.setText("");
    5.             //loop autofocus for positive focus
    6.             for (int i = 0; i < 5 && !success; i++)
    7.             {
    8.                 mCamera.autoFocus(myAutoFocusCallback);    
    9.                 String currText = tvDebug.getText().toString();
    10.                
    11.                 String combine = currText + " \n " + String.valueOf(i) + " = " + String.valueOf(success);
    12.                
    13.                 tvDebug.setText(combine);
    14.                
    15.                 try {
    16.                     Thread.sleep(1500);
    17.                 } catch (InterruptedException e) {
    18.                     // TODO Auto-generated catch block
    19.                 }
    20.             }
    21.  
    Second, the reason is doesn't write down any lines until it's done, is because you run it in the UI thread, which means that the UI is frozen until the code block is finished running.If you want to see live update, you must run the code block in a separate thread.

    Painless Threading | Android Developers
     
    pietjuhhh1990 likes this.
Loading...

Share This Page

Loading...