android surfacetexture 用法,谈谈Android中的SurfaceTexture

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 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值