相機
建立 View
camera.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
建立 Java
CameraView.java
package tw.com.shopingmemu.View;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import tw.com.shopingmemu.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
public class CameraView extends Activity implements OnClickListener, Callback,
AutoFocusCallback {
private final String FILE_PATH = "/ShopingMemu";
private final String SD_PATH = android.os.Environment
.getExternalStorageDirectory().getAbsolutePath();
private File myFilePath;
boolean isClicked = false;
SurfaceView mySurfaceView;
SurfaceHolder holder;
Camera myCamera;
private int prodcutSerial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 去除程式標題
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 去除手機狀態列
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setContentView(R.layout.camera);
Bundle extras = getIntent().getExtras();
prodcutSerial = extras.getInt("prodcutSerial");
myFilePath = new File(SD_PATH + FILE_PATH + "/" + prodcutSerial
+ ".jpg");
// 取的SurfaceView物件
mySurfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
// 取得Holder
holder = mySurfaceView.getHolder();
// 添加回傳
holder.addCallback(this);
// 設置類型
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// 設置監聽
mySurfaceView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (!isClicked) {
myCamera.autoFocus(this);// 自動對焦
isClicked = true;
} else {
Intent intent = new Intent();
CameraView.this.setResult(RESULT_OK, intent);
finish();
}
}
// 建立jpeg圖片
PictureCallback jpeg = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {// 取得圖片
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10;
// 旋轉90度
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length,
options);
int width = bm.getWidth();
int height = bm.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(width, height);
matrix.setRotate(90);
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, true);
// 釋放Bitmap佔用資源
bm.recycle();
bm = null;
File file = new File(myFilePath.getAbsolutePath());
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file));
newbm.compress(Bitmap.CompressFormat.JPEG, 100, bos);// 將圖片壓縮
bos.flush();// 輸出
bos.close();// 關閉
// 釋放Bitmap佔用資源
newbm.recycle();
newbm = null;
Toast.makeText(CameraView.this, "儲存圖片", Toast.LENGTH_LONG)
.show();
surfaceDestroyed(holder);
} catch (Exception e) {
e.printStackTrace();
}
}
};
@Override
/**
* 當surfaceView發生改變後使用
*/
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// 設置參數並開始預覽
// 建構Camera.Parameters對相機的參數進行設置
Camera.Parameters params = myCamera.getParameters();
// 設置拍照的圖片格式
params.setPictureFormat(PixelFormat.JPEG);
// 設置Preview的尺寸
// 只可以選擇(176*144,320*240,352*288,480*360,640*480)
params.setPreviewSize(640, 480);
// 設置相機採用Parameters
myCamera.setParameters(params);
// 開始預覽
myCamera.startPreview();
}
@Override
/**
* 當Surface被建立後使用
*/
public void surfaceCreated(SurfaceHolder holder) {
// 開啟相機
if (myCamera == null) {
myCamera = Camera.open();
try {
myCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
/**
* 當SurfaceView被停用時使用
*/
public void surfaceDestroyed(SurfaceHolder holder) {
// 關閉預覽並釋放資源
if (myCamera != null) {
myCamera.stopPreview();
myCamera.release();
myCamera = null;
}
Intent intent = new Intent();
CameraView.this.setResult(RESULT_OK, intent);
finish();
}
@Override
/**
* 自動對焦
*/
public void onAutoFocus(boolean success, Camera camera) {
if (success) {
// 設置參數,並拍照
Camera.Parameters params = myCamera.getParameters();
params.setPictureFormat(PixelFormat.JPEG);
params.setPreviewSize(640, 480);
myCamera.setParameters(params);
myCamera.takePicture(null, null, jpeg);
}
}
}
設定權限
<!-- 相機權限 -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- 自動對焦 -->
<uses-feature android:name="android.hardware.camera.autofocus" />
<!-- 在SD Card中寫入資料的權限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />