在我的应用场景中,我需要从camera读取图像流,然后对其处理,最后将处理结果显示到手机屏幕上。因此我写了一个GameDisplay类用于处理以上所有事务,而在MainActivity中,只需要创建一个GameDisplay类的实例并且初始化即可。
1 package com.song.camgame; 2 3 import java.io.IOException; 4 import java.util.List; 5 import java.util.Timer; 6 import java.util.TimerTask; 7 8 import android.app.Activity; 9 import android.content.Context; 10 import android.graphics.Bitmap; 11 import android.graphics.Canvas; 12 import android.graphics.ImageFormat; 13 import android.graphics.Rect; 14 import android.graphics.SurfaceTexture; 15 import android.hardware.Camera; 16 import android.hardware.Camera.Size; 17 import android.util.Log; 18 import android.view.SurfaceHolder; 19 import android.view.SurfaceView; 20 21 public class GameDisplay extends SurfaceView implements SurfaceHolder.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 public SurfaceHolder gHolder; 33 public SurfaceTexture gSurfaceTexture; 34 public Camera gCamera; 35 public byte gBuffer[]; 36 public int textureBuffer[]; 37 public ProcessThread gProcessThread; 38 private int bufferSize; 39 private Camera.Parameters parameters; 40 public int previewWidth, previewHeight; 41 public int screenWidth, screenHeight; 42 public Bitmap gBitmap; 43 private Rect gRect; 44 // timer 45 private Timer sampleTimer; 46 private TimerTask sampleTask; 47 48 public GameDisplay(Context context,int screenWidth,int screenHeight) { 49 super(context); 50 gHolder=this.getHolder(); 51 gHolder.addCallback(this); 52 gHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 53 gSurfaceTexture=new SurfaceTexture(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 @Override 61 public void surfaceChanged(SurfaceHolder holder, int format, int width, 62 int height) { 63 Log.v(TAG, "GameDisplay surfaceChanged"); 64 parameters = gCamera.getParameters(); 65 List<Size> 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 = Math 70 .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 @Override 93 public void surfaceCreated(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 @Override 103 public void surfaceDestroyed(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 @Override 115 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<textureBuffer.length;i++) 120 textureBuffer[i]=0xff000000|data[i]; 121 gBitmap.setPixels(textureBuffer, 0, previewWidth, 0, 0, previewWidth, previewHeight); 122 synchronized (gHolder) 123 { 124 Canvas canvas = this.getHolder().lockCanvas(); 125 canvas.drawBitmap(gBitmap, null,gRect, null); 126 //canvas.drawBitmap(textureBuffer, 0, screenWidth, 0, 0, screenWidth, screenHeight, false, null); 127 this.getHolder().unlockCanvasAndPost(canvas); 128 } 129 130 } 131 132 public void sampleStart() { 133 Log.v(TAG, "GameDisplay sampleStart"); 134 sampleTimer = new Timer(false); 135 sampleTask = new TimerTask() { 136 @Override 137 public void run() { 138 gProcessThread.timer=true; 139 } 140 }; 141 sampleTimer.schedule(sampleTask,0, 80); 142 } 143 }
以上程序中115-130行是最重要的部分,data是从SurfaceTexture获得的camera图像帧的拷贝,119-121行对其进行了简单的处理,122-128行将处理过的图像数据传递给负责显示的SurfaceView并显示出来。
MainActivity对GameDisplay的调用如下:
1 //声明 2 private GameDisplay gameDisplay; 3 //初始化 4 gameDisplay.setVisibility(SurfaceView.VISIBLE); 5 DisplayMetrics dm = getResources().getDisplayMetrics(); 6 screenWidth = dm.widthPixels; 7 screenHeight = dm.heightPixels; 8 gameDisplay= new GameDisplay(this,dm.widthPixels,dm.heightPixels); 9 //加入到当前activity的layout中 10 FrameLayout root = (FrameLayout) findViewById(R.id.root); 11 root.addView(gameDisplay,0);