Code example for Camera

Methods: releasesetErrorCallbacksetPreviewDisplaystartPreview, sleep

0
			if(this.mActivity.isFinishing()) return;
			this.ensureCameraDevice();
			if(this.mPreviewing) this.stopPreview();
			this.setPreviewDisplay(this.mSurfaceHolder);
			this.setCameraParameters();
			this.mCameraDevice.setErrorCallback(mErrorCallback);
			try { 
	            Log.v(TAG, "startPreview");
	            this.mCameraDevice.startPreview();
	            /************************************************ 
		         * Hack for when preview was technically started (but 
		         * probably not all the way started) and autofocus was  
		         * crashing the camera hardware.That or I wrote crappy 
		         * code somewhere.  Super rare crash but the worst kind. 
		         *************************************************/ 
		        Thread.sleep(100);
	        } catch (Throwable ex) {
	            this.closeCamera();
	            throw new RuntimeException("startPreview failed", ex);
	        } 
	         
	         
	        this.mPreviewing = true;
	        this.mCapturing = false;
	         
	         
		} catch (Exception e) {
			DialogHelper.displayFatalCameraError(this.mActivity);
	    } 
	} 
	 
	private void setCameraParameters(){ 
    	this.mParameters = this.mCameraDevice.getParameters();
    	 
    	List<Integer> frameRates = this.mParameters.getSupportedPreviewFrameRates();
    	if (frameRates != null) {
            Integer max = Collections.max(frameRates);
            this.mParameters.setPreviewFrameRate(max);
        } 
    	 
    	//If I understand this correctly all camera sizes are landscape and than 
		//a rotation is applied.  So we just need to find the optimal landscape size. 
		//That's if I understand correctly... 
		//Basically we're trying to get a decent but small picture size for faster image capturing. 
    	 
    	double ratio = (double)8/3;
    	Display display = this.mActivity.getWindowManager().getDefaultDisplay();
    	if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
    		ratio = (double)display.getWidth() / display.getHeight();
    	else 
    		ratio = (double)display.getHeight() / display.getWidth();
    	 
		int optPicWidth = (int)this.getResources().getDimension(R.dimen.camera_pic_size);
		int optPicHeight = (int) (optPicWidth / ratio);
		 
		Size optPicSize = this.getOptimalSize(this.mParameters.getSupportedPictureSizes(), optPicWidth, optPicHeight);
		Size optPreviewSize = this.getOptimalSize(this.mParameters.getSupportedPreviewSizes(), optPicSize.width, optPicSize.height);
		 
		this.mParameters.setPictureSize(optPicSize.width, optPicSize.height);
		this.mParameters.setPreviewSize(optPreviewSize.width, optPreviewSize.height);
		this.mParameters.setPictureFormat(ImageFormat.JPEG);
        this.mParameters.setJpegQuality(JPEGQUALITY);
    	 
        // Set the preview frame aspect ratio according to the picture size. 
		//PreviewFrameLayout frameLayout = (PreviewFrameLayout) findViewById(R.id.frame_layout); 
        if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
        	this.mParameters.setRotation(0);
        	//frameLayout.setAspectRatio((double) optPicSize.width / optPicSize.height); 
        } 
		else{ 
			this.mParameters.setRotation(90);
			//frameLayout.setAspectRatio((double) optPicSize.height / optPicSize.width); 
		} 
         
        String focusmode = GSSettings.getShouldAutoFocus() ? ENABLEDAUTOFOCUSSETTING : DISABLEDAUTOFOCUSSETTING;
		if(this.isSupported(focusmode, this.mParameters.getSupportedFocusModes()))
			this.mParameters.setFocusMode(focusmode);
		 
		//Do I care about exposure? 
        this.mCameraDevice.setParameters(this.mParameters);
    } 
	 
	 
	private void setPreviewDisplay(SurfaceHolder holder) {
        try { 
        	this.mCameraDevice.setPreviewDisplay(holder);
        } catch (Throwable ex) {
        	this.closeCamera();
            throw new RuntimeException("setPreviewDisplay failed", ex);
        } 
    } 
 
    private void ensureCameraDevice(){ 
        if (this.mCameraDevice == null) {
        	this.mCameraDevice = Camera.open();
        	if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
				this.mCameraDevice.setDisplayOrientation(0);
			else 
				this.mCameraDevice.setDisplayOrientation(90);
        } 
    } 
     
    private void closeCamera() { 
        if (this.mCameraDevice != null) {
        	this.mCameraDevice.release();
            this.mCameraDevice = null;
            this.mPreviewing = false;
        } 
    }