import java.io.ByteArrayOutputStream; java
import java.io.IOException; android
import java.util.Timer; app
import java.util.TimerTask; ide
import android.app.Activity; this
import android.content.Context; code
import android.graphics.Bitmap; orm
import android.graphics.BitmapFactory; 對象
import android.graphics.ImageFormat; get
import android.graphics.Rect; it
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.ImageView;
public class CameraTestActivity extends Activity implements SurfaceHolder.Callback {
private static String TAG = CameraTestActivity.class.getSimpleName();
private SurfaceHolder surfaceHolder;
private Camera camera;
private ImageView imageView;
private Timer mTimer;
private TimerTask mTimerTask;
private Camera.AutoFocusCallback mAutoFocusCallBack;
private Camera.PreviewCallback previewCallback;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
imageView = (ImageView) findViewById(R.id.image_view);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mAutoFocusCallBack = new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
if (success) {
// isAutoFocus = true;
camera.setOneShotPreviewCallback(previewCallback);
Log.d(TAG, "onAutoFocus success");
}
}
};
previewCallback = new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera arg1) {
if (data != null)
{
Camera.Parameters parameters = camera.getParameters();
int imageFormat = parameters.getPreviewFormat();
Log.i("map", "Image Format: " + imageFormat);
Log.i("CameraPreviewCallback", "data length:" + data.length);
if (imageFormat == ImageFormat.NV21)
{
// get full picture
Bitmap image = null;
int w = parameters.getPreviewSize().width;
int h = parameters.getPreviewSize().height;
Rect rect = new Rect(0, 0, w, h);
YuvImage img = new YuvImage(data, ImageFormat.NV21, w, h, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (img.compressToJpeg(rect, 100, baos))
{
image = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.size());
imageView.setImageBitmap(image);
}
}
}
}
};
mTimer = new Timer();
mTimerTask = new CameraTimerTask();
mTimer.schedule(mTimerTask, 0, 500);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
initCamera();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
previewCallback = null;
mAutoFocusCallBack = null;
}
public void initCamera() {
camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); // 獲取當前屏幕管理器對象
Display display = wm.getDefaultDisplay(); // 獲取屏幕信息的描述類
parameters.setPreviewSize(display.getWidth(), display.getHeight());
camera.setParameters(parameters);
try {
camera.setPreviewDisplay(surfaceHolder);
} catch (IOException e) {
System.out.println(e.getMessage());
}
camera.startPreview();
}
class CameraTimerTask extends TimerTask {
@Override
public void run() {
if (camera != null) {
camera.autoFocus(mAutoFocusCallBack);
}
}
}
}