1. 程式人生 > >Android自動聚焦、攝像頭拍照、縮放至標準大小的完整實現

Android自動聚焦、攝像頭拍照、縮放至標準大小的完整實現

手機華為U9200,

android4.0.3,

預覽的引數:myParameters.setPreviewSize(1280, 720)

圖片引數:myParameters.setPictureSize(2048, 1152); //1280, 720

圖片最終尺寸:寬600 * 高800

         參照http://www.cnblogs.com/liuan/archive/2012/01/10/2318300.html,上面的思路是弄了一個定時器來進行自動聚焦,但發現根本不中,一按拍照程式就掛掉。而且由於定時器一直在重複迴圈,相機就在那聚焦來聚焦去,也就是一下清晰一下又模糊了。後來我把程式改成,當拍照後把定時器關閉掉髮現還是不中。經反覆實驗,終於成功。南無阿彌陀佛,本著我為人人,人人為我。將原始碼貼在此處:

第一,佈局檔案。裡面有一個surfaceview和三個按鈕,分別是預覽、拍照、儲存。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<SurfaceView
    android:id="@+id/mySurfaceView"  
    android:layout_width="fill_parent"
    android:background="#D1EEEE"
    android:layout_height="800px"
    android:gravity="center" />

<LinearLayout
    android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingTop="20dip"    
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btnPreview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:text="預覽" />

    <Button
        android:id="@+id/btnPhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="拍照" />

    <Button
        android:id="@+id/btnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="儲存" />

</LinearLayout>

</LinearLayout>

第2,源程式:

package yan.guoqi.testphoto;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class TestPhotoActivity extends Activity implements SurfaceHolder.Callback{
    /** Called when the activity is first created. */
	private static final  String TAG = "yan:";
	SurfaceView mySurfaceView = null;
	SurfaceHolder mySurfaceHolder = null;
	Button btnPreview = null;
	Button btnPhoto = null;
	Button btnSave = null;
	Camera myCamera = null;
	Camera.Parameters myParameters;
	boolean isView = false;
	Bitmap bm;
	String savePath = "/mnt/sdcard/testPhoto/";
	int cntSave = 0;
	private Camera.AutoFocusCallback mAutoFocusCallback;
	//private Camera.PreviewCallback mPreviewCallback;
	private Timer mTimer;
	private TimerTask mTimerTask;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	//璁劇疆鍏ㄥ睆鏃犳爣棰?    	requestWindowFeature(Window.FEATURE_NO_TITLE);
    	int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
    	Window myWindow = this.getWindow();
    	myWindow.setFlags(flag, flag);
    	setContentView(R.layout.main); //璁劇疆甯冨矓
    	mySurfaceView = (SurfaceView)findViewById(R.id.mySurfaceView);
    	mySurfaceView.setZOrderOnTop(true);
    	mySurfaceHolder = mySurfaceView.getHolder();
    	mySurfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
    	btnPreview = (Button)findViewById(R.id.btnPreview);
    	btnPhoto = (Button)findViewById(R.id.btnPhoto);
    	btnSave = (Button)findViewById(R.id.btnSave);
    	if(!isFolderExist(savePath)) //濡傛灉鍒涘緩鏂囦歡澶瑰け璐?    	{
    		AlertDialog.Builder alertDialog = new Builder(TestPhotoActivity.this);
    		alertDialog.setTitle("閿欒");
    		alertDialog.setMessage("鍥劇墖淇濆瓨鏂囦歡澶瑰壋寤哄け璐ワ紒");
    		alertDialog.setPositiveButton("紜畾", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.dismiss();
					TestPhotoActivity.this.finish();
				}
			});
    		alertDialog.show();
    	}
    	else
    		Toast.makeText(TestPhotoActivity.this,
    				"鎮ㄧ殑鐓х墖灝嗕繚瀛樺湪錛? + savePath,
    				Toast.LENGTH_SHORT).show();
    	mySurfaceHolder.addCallback(this);
    	mySurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    	mAutoFocusCallback = new Camera.AutoFocusCallback() {
			
			public void onAutoFocus(boolean success, Camera camera) {
				// TODO Auto-generated method stub
				if(success){
					myCamera.setOneShotPreviewCallback(null);
		    		Toast.makeText(TestPhotoActivity.this,
		    				"鑷姩鑱氱劍鎴愬姛錛?,
		    				Toast.LENGTH_SHORT).show();
				}
					
			}
		}; 
		mTimer = new Timer();
		mTimerTask = new CameraTimerTask();
		//mTimer.schedule(mTimerTask, 0, 500);
		
    	btnPreview.setOnClickListener(new BtnListener());
    	btnPhoto.setOnClickListener(new BtnListener());
    	btnSave.setOnClickListener(new BtnListener());
    	
    }
    ShutterCallback myShutterCallback = new ShutterCallback() {
		
		public void onShutter() {
			// TODO Auto-generated method stub
			
		}
	};
	PictureCallback myRawCallback = new PictureCallback() {
		
		public void onPictureTaken(byte[] data, Camera camera) {
			// TODO Auto-generated method stub
			
		}
	};
	PictureCallback myjpegCalback = new PictureCallback() {
		
		public void onPictureTaken(byte[] data, Camera camera) {
			// TODO Auto-generated method stub
			Log.i(TAG, "onPictureTaken........");
			bm = BitmapFactory.decodeByteArray(data, 0, data.length);
			isView = false;
			myCamera.stopPreview();
			myCamera.release();
			myCamera = null;
			isView = false;
			
		}
	};
    class BtnListener implements OnClickListener{

		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch(v.getId()){
			case R.id.btnPreview:
				Toast.makeText(TestPhotoActivity.this,
						"鎮ㄦ寜浜嗛瑙堟寜閽?,
						Toast.LENGTH_SHORT).show();
				initCamera();
				break;
			case R.id.btnPhoto:
				if(isView && myCamera!=null){	
										
					myCamera.takePicture(myShutterCallback, myRawCallback, myjpegCalback);
				}
				else
					Toast.makeText(TestPhotoActivity.this, "璇峰厛棰勮鐒跺悗鎷嶇収錛?, Toast.LENGTH_SHORT).show();				
				break;
			case R.id.btnSave:
                  if(bm == null)
                  {
                	  Toast.makeText(TestPhotoActivity.this, "璇鋒媿鎽勬垚鍔熷悗鍐嶄繚瀛橈紒錛侊紒", Toast.LENGTH_SHORT).show();
                	  return;
                  }
      			int b =0, c=1;
    			File fTest = new File(savePath + b + c + ".JPG");
    			while(fTest.exists()){
    				if(c==9){
    					b++;
    					c = 0;
    				}
    				else
    					c++;
    				if(b==9){
    					b = 0;
    					Toast.makeText(TestPhotoActivity.this, "姝ゅ織鎰胯€呮牱鏈暟鐩凡瓚呰繃100錛?, 
    							Toast.LENGTH_SHORT).show();
    				}
    				fTest = new File(savePath + b + c + ".JPG");
    			}
    			try {
					FileOutputStream fout = new FileOutputStream(fTest);
					BufferedOutputStream bos = new BufferedOutputStream(fout);
					Bitmap mBitmap = Bitmap.createScaledBitmap(bm, 600, 800, false);
					mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
					//bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
					bos.flush();
					bos.close();
					cntSave++;
					Toast.makeText(TestPhotoActivity.this, "鎮ㄦ媿鐨勭"+cntSave+"寮爌icture淇濆瓨鎴愬姛錛佺紪鍙鳳細"+ b + c,
							Toast.LENGTH_SHORT).show();
					
				}  catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Toast.makeText(TestPhotoActivity.this,
							"淇濆瓨澶辮觸",
							Toast.LENGTH_SHORT).show();
				}
    			
                	  
				break;
				default:									
				
			}			
		}    	
    }
    
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		
	}

	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		
		
	}
	
	//鍒濆鍖栨憚鍍忓ご
	public void initCamera()
	{
		if(myCamera == null && !isView)
		{
			myCamera = Camera.open();
			Log.i(TAG, "camera.open");
		}
		if(myCamera != null && !isView) {
			try {
				
					myParameters = myCamera.getParameters();
					myParameters.setPictureFormat(PixelFormat.JPEG);
					myParameters.setPreviewSize(1280, 720);
					//myParameters.setFocusMode("auto");
					
					myParameters.setPictureSize(2048, 1152); //1280, 720
					
					myParameters.set("rotation", 90);
					myCamera.setDisplayOrientation(90);
					myCamera.setParameters(myParameters);
					myCamera.setPreviewDisplay(mySurfaceHolder);
					myCamera.startPreview();
					isView = true;
					myCamera.autoFocus(mAutoFocusCallback);
			
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				Toast.makeText(TestPhotoActivity.this, "鍒濆鍖栫溮鏈洪敊璇?,
						Toast.LENGTH_SHORT).show();
			}
		}
		
	}
	class CameraTimerTask extends TimerTask{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			if(myCamera != null)
			{
				myCamera.autoFocus(mAutoFocusCallback);
			}
			
		}
		
	}
	//鍒ゆ柇鏂囦歡澶規槸鍚﹀瓨鍦紝濡傛灉涓嶅瓨鍦ㄥ墶鍒涘緩涓€涓?		public boolean isFolderExist(String folderPath){
			boolean result = false;
			File f = new File(folderPath);
			if(!f.exists()){
				if(f.mkdir()){
					result = true;
				}
				else
					result = false;
			}
			else
				result = true;
			
			return result;
		}
}

自動聚焦部分,核心的程式碼有,建構函式裡的

private Camera.AutoFocusCallback mAutoFocusCallback;
mAutoFocusCallback = new Camera.AutoFocusCallback() {
   
   public void onAutoFocus(boolean success, Camera camera) {
    // TODO Auto-generated method stub
    if(success){
     myCamera.setOneShotPreviewCallback(null);
        Toast.makeText(TestPhotoActivity.this,
          "自動聚焦成功" , Toast.LENGTH_SHORT).show();
    }
     
   }
  };
和initCamera裡的myCamera.autoFocus(mAutoFocusCallback);呼叫!

第三,AndroidManifest檔案,紅線部分是新增的對攝像頭操作、sd儲存的許可權許可。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="yan.guoqi.testphoto"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="15" />
    <!-- 鍦╯d鍗′腑鍒涘緩鍜屽垹闄ゆ枃浠剁殑鏉冮檺 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <!-- 鍦╯d鍗′腑鍐欏靉鏁版嵁鐨勬潈闄?-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- 浣跨敤鎷嶇収鐨勬潈闄?-->
    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.camera" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".TestPhotoActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


   上兩張效果圖,來看下攝像頭相同的previewSize、pictureSize下,自動對焦前後的差別:



效果差別不是一般的大啊!

1,上面的源程式裡mTimer、mTimerTask等這些是我第一種思路,用定時器來控制聚焦,結果是失敗的。碰到這些,大家略過去就行了。

2,這裡控制聚焦的核心實現時,在initCamera函式裡,執行myCamera.autoFocus(mAutoFocusCallback);自動聚焦這句話!在建構函式裡定義了一個mAutoFocusCallback的回撥函式變數。還可以在按下拍照按鈕後判斷當前是否自動聚焦成功,如果聚焦成功再拍照!mAutoFocusCallback裡的if(success)就是標示自動聚焦成功

3,網上有很多,只是簡單的在建構函式裡寫一句myCamera.setFocus(null),我真懷疑,這不是坑爹麼?反正我這樣是沒弄成功。

4,程式裡的亂碼部分是文字,由於從linux下拷出來就成亂碼了,大家不用太關心,都是些提示性的話。

5,另外還要交代一下,自動聚焦這句話一定要在攝像頭正常預覽的時候呼叫,否則是沒有意義的,程式也會掛掉。

6,我核心的參考文章:

向他們表示感謝! 

遺留的問題:

我第一次按下預覽按鈕後,會進行自動聚焦,並提示自動聚焦成功。如果此時執行拍照--儲存--再次預覽,則自動聚焦是成功的。 如果我預覽後,調整了和拍攝物的距離這時想再次自動聚焦,即連續兩次按下預覽按鈕,怎麼第二次就不自動聚焦了呢???這是不是和android攝像頭內建的屬性有關係?

                                                                                                                                                                                                          ----------------------作者yanzi1225627 轉載請註明 2012.8.31