1 packagecom.song.camgame;2
3 importjava.io.IOException;4 importjava.util.List;5 importjava.util.Timer;6 importjava.util.TimerTask;7
8 importandroid.app.Activity;9 importandroid.content.Context;10 importandroid.graphics.Bitmap;11 importandroid.graphics.Canvas;12 importandroid.graphics.ImageFormat;13 importandroid.graphics.Rect;14 importandroid.graphics.SurfaceTexture;15 importandroid.hardware.Camera;16 importandroid.hardware.Camera.Size;17 importandroid.util.Log;18 importandroid.view.SurfaceHolder;19 importandroid.view.SurfaceView;20
21 public class GameDisplay extends SurfaceView implementsSurfaceHolder.Callback,22 Camera.PreviewCallback{23 public final static String TAG="GameDisplay";24
25 private static final int MAGIC_TEXTURE_ID = 10;26 public static final int DEFAULT_WIDTH=800;27 public static final int DEFAULT_HEIGHT=480;28 public static final int BLUR = 0;29 public static final int CLEAR = BLUR + 1;30 //public static final int PAUSE = PLAY + 1;31 //public static final int EXIT = PAUSE + 1;
32 publicSurfaceHolder gHolder;33 publicSurfaceTexture gSurfaceTexture;34 publicCamera gCamera;35 public bytegBuffer[];36 public inttextureBuffer[];37 publicProcessThread gProcessThread;38 private intbufferSize;39 privateCamera.Parameters parameters;40 public intpreviewWidth, previewHeight;41 public intscreenWidth, screenHeight;42 publicBitmap gBitmap;43 privateRect gRect;44 //timer
45 privateTimer sampleTimer;46 privateTimerTask sampleTask;47
48 public GameDisplay(Context context,int screenWidth,intscreenHeight) {49 super(context);50 gHolder=this.getHolder();51 gHolder.addCallback(this);52 gHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);53 gSurfaceTexture=newSurfaceTexture(MAGIC_TEXTURE_ID);54 this.screenWidth=screenWidth;55 this.screenHeight=screenHeight;56 gRect=new Rect(0,0,screenWidth,screenHeight);57 Log.v(TAG, "GameDisplay initialization completed");58 }59
60 @Override61 public void surfaceChanged(SurfaceHolder holder, int format, intwidth,62 intheight) {63 Log.v(TAG, "GameDisplay surfaceChanged");64 parameters =gCamera.getParameters();65 List preSize =parameters.getSupportedPreviewSizes();66 previewWidth = preSize.get(0).width;67 previewHeight = preSize.get(0).height;68 for (int i = 1; i < preSize.size(); i++) {69 double similarity =Math70 .abs(((double) preSize.get(i).height /screenHeight)71 - ((double) preSize.get(i).width /screenWidth));72 if (similarity < Math.abs(((double) previewHeight /screenHeight)73 - ((double) previewWidth /screenWidth))) {74 previewWidth =preSize.get(i).width;75 previewHeight =preSize.get(i).height;76 }77 }78 gBitmap=Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);79 parameters.setPreviewSize(previewWidth, previewHeight);80 gCamera.setParameters(parameters);81 bufferSize = previewWidth *previewHeight;82 textureBuffer=new int[bufferSize];83 bufferSize = bufferSize * ImageFormat.getBitsPerPixel(parameters.getPreviewFormat()) / 8;84 gBuffer = new byte[bufferSize];85 gCamera.addCallbackBuffer(gBuffer);86 gCamera.setPreviewCallbackWithBuffer(this);87 gCamera.startPreview();88 //gProcessThread = new ProcessThread(surfaceView,handler,null,previewWidth,previewHeight);89 //processThread.start();
90 }91
92 @Override93 public voidsurfaceCreated(SurfaceHolder holder) {94 Log.v(TAG, "GameDisplay surfaceCreated");95 if (gCamera == null) {96 gCamera =Camera.open();97 }98 gCamera.setPreviewTexture(gSurfaceTexture);99 //sampleStart();
100 }101
102 @Override103 public voidsurfaceDestroyed(SurfaceHolder holder) {104 Log.v(TAG, "GameDisplay surfaceDestroyed");105 //gProcessThread.isRunning=false;106 //sampleTimer.cancel();107 //sampleTimer = null;108 //sampleTask.cancel();109 //sampleTask = null;
110 gCamera.stopPreview();111 gCamera.release();112 }113
114 @Override115 public void onPreviewFrame(byte[] data, Camera camera) {116 Log.v(TAG, "GameDisplay onPreviewFrame");117 //gProcessThread.raw_data=data;
118 camera.addCallbackBuffer(gBuffer);119 for(int i=0;i
127 this.getHolder().unlockCanvasAndPost(canvas);128 }129
130 }131
132 public voidsampleStart() {133 Log.v(TAG, "GameDisplay sampleStart");134 sampleTimer = new Timer(false);135 sampleTask = newTimerTask() {136 @Override137 public voidrun() {138 gProcessThread.timer=true;139 }140 };141 sampleTimer.schedule(sampleTask,0, 80);142 }143 }