当前位置:操作系统 > 安卓/Android >>

Android自动聚焦、摄像头拍照、缩放至标准大小的完整实现

第一,布局文件。里面有一个surfaceview和三个按钮,分别是预览、拍照、保存。

 

[html]
<?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,源程序:

[cpp]
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) { 
             &nbs

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,