- package com.yarin.android.Examples_07_06;
-
-
import java.util.Timer;
-
import java.util.TimerTask;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.util.Log;
-
import android.view.View;
-
import android.view.Window;
-
-
public class Activity01 extends Activity
-
{
-
-
/* (non-Javadoc)
-
* @see android.app.Activity#onDestroy()
-
*/
-
@Override
-
protected void onDestroy() {
-
if( timer != null){
-
timer.cancel();
-
}
-
// TODO Auto-generated method stub
-
super.onDestroy();
-
}
-
-
/* (non-Javadoc)
-
* @see android.app.Activity#onResume()
-
*/
-
@Override
-
protected void onResume() {
-
// TODO Auto-generated method stub
-
super.onResume();
-
}
-
-
Timer timer = new Timer();
-
-
/** Called when the activity is first created. */
-
@Override
-
public void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
requestWindowFeature(Window.FEATURE_NO_TITLE);
-
-
setContentView(R.layout.main);
-
-
}
-
-
public void Test(View view)
-
{
-
timer.schedule(new TimerTask()
-
{
-
public void run() {
-
Log.v("CA", "TimerTask");
-
Intent intent = new Intent();
-
intent.setClass(Activity01.this, CameraActivity.class);
-
Activity01.this.startActivity(intent);
-
}
-
}, 5000, 2000);
-
}
-
-
public void Stop(View view)
-
{
-
this.timer.cancel();
-
this.finish();
-
}
-
}
- package com.yarin.android.Examples_07_06;
-
-
import java.io.BufferedOutputStream;
-
import java.io.File;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
-
import android.app.Activity;
-
import android.content.Context;
-
import android.graphics.Bitmap;
-
import android.graphics.BitmapFactory;
-
import android.graphics.Canvas;
-
import android.graphics.PixelFormat;
-
import android.hardware.Camera;
-
import android.hardware.Camera.PictureCallback;
-
import android.os.Bundle;
-
//import android.view.KeyEvent;
-
import android.util.Log;
-
import android.view.SurfaceHolder;
-
import android.view.SurfaceView;
-
import android.view.Window;
-
-
public class CameraActivity extends Activity {
-
-
private Preview mPreview = null;
-
-
/* (non-Javadoc)
-
* @see android.app.Activity#onPause()
-
*/
-
@Override
-
protected void onPause() {
-
Log.v("CA", "onPause");
-
if( mPreview != null ){
-
mPreview = null;
-
}
-
-
super.onPause();
-
}
-
-
/* (non-Javadoc)
-
* @see android.app.Activity#onResume()
-
*/
-
@Override
-
protected void onResume() {
-
// Create our Preview view and set it as the content of our activity.
-
Log.v("CA", "onResume");
-
-
super.onResume();
-
}
-
-
-
/** Called when the activity is first created. */
-
@Override
-
public void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
requestWindowFeature(Window.FEATURE_NO_TITLE);
-
-
mPreview = new Preview(this);
-
setContentView(mPreview);
-
}
-
-
-
-
/* Preview-显示Preview */
-
class Preview extends SurfaceView implements SurfaceHolder.Callback
-
{
-
SurfaceHolder mHolder = null;
-
Camera mCamera = null;
-
Bitmap CameraBitmap = null;
-
-
Preview(Context context)
-
{
-
super(context);
-
mHolder = getHolder();
-
mHolder.addCallback(this);
-
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
-
}
-
-
public void startCamera() {
-
Log.v("CA", "startCamera");
-
-
if (mCamera == null) {
-
try {
-
mCamera = Camera.open();
-
Log.v("CA", "Camera.open");
-
} catch (Exception e) {
-
}
-
-
} else {
-
try {
-
mCamera.reconnect();
-
} catch (IOException e) {
-
mCamera.release();
-
mCamera = null;
-
}
-
}
-
-
}
-
-
public void stopCamera() {
-
Log.v("CA", "stopCamera");
-
-
if (mCamera != null) {
-
try {
-
mCamera.stopPreview();
-
} catch (Exception e) {
-
// Ignore
-
}
-
-
try {
-
mCamera.release();
-
Log.v("CA", "mCamera.release");
-
} catch (Exception e) {
-
// Ignore
-
}
-
-
mCamera = null;
-
}
-
}
-
-
@Override
-
public void surfaceCreated(SurfaceHolder holder)
-
{
-
Log.v("CA", "surfaceCreated");
-
-
startCamera();
-
-
if( mCamera == null ) return;
-
-
Log.v("CA", "Camera create!");
-
-
try {
-
mCamera.setPreviewDisplay(holder);
-
} catch (IOException e) {
-
mCamera.release();
-
mCamera = null;
-
e.printStackTrace();
-
}
-
}
-
-
@Override
-
public void surfaceDestroyed(SurfaceHolder holder)
-
{
-
Log.v("CA", "surfaceDestroyed");
-
stopCamera();
-
}
-
-
@Override
-
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
-
{
-
Log.v("CA", "surfaceChanged");
-
if( holder.isCreating() && mCamera != null ){
-
-
if( mCamera != null ){
-
/* 构建Camera.Parameters对相机的参数进行设置 */
-
//Camera.Parameters parameters = mCamera.getParameters();
-
/* 设置拍照的图片格式 */
-
//parameters.setPictureFormat(PixelFormat.JPEG);
-
/* 设置Preview的尺寸 */
-
//parameters.setPreviewSize(320, 480);
-
/* 设置图像分辨率 */
-
//parameters.setPictureSize(320, 480);
-
/* 设置相机采用parameters */
-
//mCamera.setParameters(parameters);
-
/* 开始预览 */
-
mCamera.startPreview();
-
-
CameraActivity.this.finish();
-
}
-
}
-
}
-
/* 拍照片 */
-
public void takePicture()
-
{
-
if (mCamera != null)
-
{
-
mCamera.takePicture(null, null, jpegCallback);
-
}
-
}
-
/* 拍照后输出图片 */
-
private PictureCallback jpegCallback = new PictureCallback()
-
{
-
public void onPictureTaken(byte[] _data, Camera _camera)
-
{
-
// TODO Handle JPEG image data
-
CameraBitmap = BitmapFactory.decodeByteArray(_data, 0, _data.length);
-
File myCaptureFile = new File("/sdcard/camera1.jpg");
-
try
-
{
-
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
-
CameraBitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
-
bos.flush();
-
bos.close();
-
/* 将拍到的图片绘制出来 */
-
Canvas canvas= mHolder.lockCanvas();
-
canvas.drawBitmap(CameraBitmap, 0, 0, null);
-
mHolder.unlockCanvasAndPost(canvas);
-
-
}
-
catch (Exception e)
-
{
-
e.getMessage();
-
}
-
}
-
};
-
}
-
-
}
阅读(1599) | 评论(0) | 转发(0) |