MainActivity.java
代碼
package com.amaker.ch11.app;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainActivity extends Activity {
// 聲明拍照介面組件SurfaceView
private SurfaceView surfaceView;
// 聲明介面控制組件SurfaceHolder
private SurfaceHolder surfaceHolder;
// 聲明照相機
private Camera camera;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 執行個體化拍照介面組件
surfaceView = (SurfaceView)findViewById(R.id.preview);
// 從SurfaceView中獲得SurfaceHolder
surfaceHolder = surfaceView.getHolder();
// 為SurfaceHolder 添加回調
surfaceHolder.addCallback(surfaceCallback);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
// 響應按鍵事件
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_CAMERA||keyCode==KeyEvent.KEYCODE_SEARCH){
takePic();
return true;
}
return super.onKeyDown(keyCode, event);
}
// 拍照方法
private void takePic(){
// 停止預覽
camera.stopPreview();
// 拍照
camera.takePicture(null,null, pictureCallback);
}
// 照片回調
Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
new SavePictureTask().execute(data);
camera.startPreview();
}
};
// 儲存照片任務類
class SavePictureTask extends AsyncTask<byte[],String,String>{
@Override
protected String doInBackground(byte[]... params) {
// 建立檔案
File picture = new File(Environment.getExternalStorageDirectory(),"picture.jpg");
// 如果檔案存在刪除掉
if(picture.exists())picture.delete();
try {
// 獲得檔案輸出資料流
FileOutputStream fos = new FileOutputStream(picture.getPath());
// 寫到該檔案
fos.write(params[0]);
// 關閉檔案流
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
// SurfaceHodler 回調,處理開啟相機,關閉相機以及照片尺寸的改變
SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
// 開啟相機
camera = Camera.open();
try {
// 設定預覽
camera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// 獲得相機參數
Camera.Parameters parameters = camera.getParameters();
// 設定照片大小
parameters.setPreviewSize(width, height);
// 設定照片格式
parameters.setPictureFormat(PixelFormat.JPEG);
// 設定相機參數
camera.setParameters(parameters);
// 開始預覽
camera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// 停止預覽
camera.stopPreview();
// 釋放相機資源
camera.release();
camera = null;
}
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.view.SurfaceView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</android.view.SurfaceView>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amaker.ch11.app"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
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>
<uses-sdk android:minSdkVersion="3" />
</manifest>