• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Loop camera autofocus

pietjuhhh1990

Well-Known Member
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:
            success = false;    
            int iLoop = 5;
            //loop autofocus for positive focus
            while (iLoop >= 0 && success == false)
            {
                iLoop--;
                mCamera.autoFocus(myAutoFocusCallback);    
                try {
                    //give camera time 2 focus
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                }
            }
Code:
    AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){
    
          public void onAutoFocus(boolean success, Camera mCamera) {
              success = true;
              return;
          }
    };
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 :)
 
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:
            success = false;
            TextView tvDebug = (TextView) findViewById(R.id.tvDebug);
            tvDebug.setText("");
            int iLoop = 5;
            //loop autofocus for positive focus
            while (iLoop >= 1 && success == false)
            {
                mCamera.autoFocus(myAutoFocusCallback);    
                String currText = tvDebug.getText().toString();
                
                String combine = currText + " \n " + String.valueOf(iLoop) + " = " + String.valueOf(success);
                
                tvDebug.setText(combine);
                
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                }

                iLoop--;
            }
and the autofocus callback, i added a returnbool but it also din't help

Code:
    AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){
    
        @Override    
          public void onAutoFocus(boolean success, Camera mCamera) {
                  success = true;
              return;
          }
        
        public boolean onAutoFocus(){
            success = true;
            return success;
        }
    };


I hope someone knows what's wrong
 
First, when you know how many times to loop, always use a for loop.
Code:
            success = false;
            TextView tvDebug = (TextView) findViewById(R.id.tvDebug);
            tvDebug.setText("");
            //loop autofocus for positive focus
            for (int i = 0; i < 5 && !success; i++)
            {
                mCamera.autoFocus(myAutoFocusCallback);    
                String currText = tvDebug.getText().toString();
                
                String combine = currText + " \n " + String.valueOf(i) + " = " + String.valueOf(success);
                
                tvDebug.setText(combine);
                
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                }
            }

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
 
Back
Top Bottom