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

Camera focus when the object is too close

Hi... I have a Samsung Galaxy S, I wrote an app to take pictures. When I try to get a close up object... the camera doesn't get the focus and doesn't take the picture.

Code:
    public void takePicture() {
		// Take picture.
		if (mIsFocused || !mPreviewing) {
			Log.d("CaptureThread", "in onSnap");
			mCaptureObject.snap(mImageCapture);
		    clearFocus();
		} else {
			Log.d("CaptureThread", "in autoFocus");
		    mCaptureOnFocus = true;
		    autoFocus();
		    clearFocus();
		}
    }

mIsFocused is defined in:
Code:
    private final class AutoFocusCallback implements android.hardware.Camera.AutoFocusCallback {
        public void onAutoFocus(boolean focused, android.hardware.Camera camera) {
            mIsFocusing = false;
            mIsFocused = focused;
            Log.d("AutoFocusCallback", "focused:" + mIsFocused);
            Log.d("AutoFocusCallback", "mCaptureOnFocus:" + mCaptureOnFocus);
            if (focused) {
                if (mCaptureOnFocus && mCaptureObject != null) {
                    // No need to play the AF sound if we're about to play the shutter sound
                    if(mCaptureObject.snap(mImageCapture)) {
                        clearFocus();
                        mCaptureOnFocus = false;
                        return;
                    }
                    clearFocus();
                }
                mCaptureOnFocus = false;
            }
        }


mPreviewing is defined in:
Code:
    private void setViewFinder(int w, int h, boolean startPreview) {
		Log.d("setViewFinder", "in setViewFinder.");
		Log.d("setViewFinder", "w=" + w + "  h=" + h);
        if (mPausing) {
    		Log.d("setViewFinder", "in mPausing.");
            return;
        }
        
        if (mPreviewing && 
                w == mViewFinderWidth && 
                h == mViewFinderHeight) {
    		Log.d("setViewFinder", "in no change size.");
            return;
        }
        
        if (!ensureCameraDevice()) {
    		Log.d("setViewFinder", "in ensureCameraDevice.");
            return;
        }
        
        if (mSurfaceHolder == null) {
    		Log.d("setViewFinder", "in mSurfaceHolder is null.");
            return;
        }
        
        if (mMainActivity.isFinishing()) {
    		Log.d("setViewFinder", "in fihishing.");
            return;
        }
        
        if (mPausing) {
    		Log.d("setViewFinder", "in mPausing.");
            return;
        }
        
        // remember view finder size
        mViewFinderWidth = w;
        mViewFinderHeight = h;

        if (startPreview == false) {
    		Log.d("setViewFinder", "in startPreview = false.");
            return;
        }

        /** 
         * start the preview if we're asked to...
         */

        // we want to start the preview and we're previewing already,
        // stop the preview first (this will blank the screen).
        if (mPreviewing)
            stopPreview();
        
        // this blanks the screen if the surface changed, no-op otherwise
        try {
        	mCameraDevice.setPreviewDisplay(mSurfaceHolder);
		} catch (Exception e) {
			e.printStackTrace();
            stopPreview();
            return;
		}
        

        // request the preview size, the hardware may not honor it,
        // if we depended on it we would have to query the size again        
        Parameters parameters = mCameraDevice.getParameters();
        //parameters.setPreviewSize(w, h);
        parameters.set("jpeg-quality", 100);
        parameters.set("orientation", "portrait");
        
		try {
			List<Camera.Size> supportedSizes = null;
			supportedSizes = Compatibility.getSupportedPreviewSizes(parameters);

			Iterator<Camera.Size> itr = supportedSizes.iterator(); 
			while(itr.hasNext()) {
				Camera.Size element = itr.next(); 
				element.width -= w;
				element.height -= h;
			} 
			Collections.sort(supportedSizes, new ResolutionsOrder());
			parameters.setPreviewSize(w + supportedSizes.get(supportedSizes.size()-1).width, h + supportedSizes.get(supportedSizes.size()-1).height);
		} catch (Exception ex) {
			parameters.setPreviewSize(320,240);
		}
        mCameraDevice.setParameters(parameters);
        
        


		Log.d("setViewFinder", "start mCameraDevice.startPreview().");
        mCameraDevice.startPreview();
        mPreviewing = true;    	
        this.startPreview();
        

    }
 
Development quiestions are best served in a development forum. This subforum is really geared towards users of apps.
 
There is a physical limit to how close the actual camera can focus. Without a physical Macro switch, you usually cannot focus on anything closer than 3-4 inches or so.
 
Back
Top Bottom