手機華为U9200,

android4.0.3,

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

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

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

關於setPreviewSize和setPictureSize的問題可以参照我以前的文章http:// blog.csdn.net/yanzi1225627/article/details/7738736

 

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

第一,布局文件。裏面有一個surfaceview和三個按鈕,分別是預覽、拍照、保存。

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://go.rritw.com/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://go.rritw.com/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下,自動對焦前後的差別:

1346378673_6468  

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

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

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

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

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

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

 

向他們表示感謝! 

遺留的問題:

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

源碼下載鏈接:http://go.rritw.com/download.csdn.net/detail/yanzi1225627/4538626

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


From:CSDN

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()