Android自动聚焦、摄像头拍照、缩放至标准大小的完整实现
第一,布局文件。里面有一个su易做图ceview和三个按钮,分别是预览、拍照、保存。
[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" >
<Su易做图ceView
android:id="@+id/mySu易做图ceView"
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.DialogInte易做图ce;
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.Su易做图ceHolder;
import android.view.Su易做图ceView;
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 Su易做图ceHolder.Callback{
/** Called when the activity is first created. */
private static final String TAG = "yan:";
Su易做图ceView mySu易做图ceView = null;
Su易做图ceHolder mySu易做图ceHolder = 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); //璁剧疆甯冨眬
mySu易做图ceView = (Su易做图ceView)findViewById(R.id.mySu易做图ceView);
mySu易做图ceView.setZOrderOnTop(true);
mySu易做图ceHolder = mySu易做图ceView.getHolder();
mySu易做图ceHolder.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 DialogInte易做图ce.OnClickListener() {
public void onClick(DialogInte易做图ce dialog, int which) {
&nbs
补充:移动开发 , Android ,