android---------SurfaceTexture----------通过SurfaceTexture来对预览的图像进行处理

package com.example.testsurfacetexture;

import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GameDisplay extends SurfaceView implements SurfaceHolder.Callback,
Camera.PreviewCallback{
    public final static String TAG="GameDisplay";
    
    private static final int MAGIC_TEXTURE_ID = 10;
    public static final int DEFAULT_WIDTH=800;
    public static final int DEFAULT_HEIGHT=480;
    public static final int BLUR = 0;
    public static final int CLEAR = BLUR + 1;
    public SurfaceHolder gHolder;
    public  SurfaceTexture gSurfaceTexture;
    public Camera gCamera;
    public byte gBuffer[];
    public int textureBuffer[];
    private int bufferSize;
    private Camera.Parameters parameters;
    public int previewWidth, previewHeight;
    public int screenWidth, screenHeight;
    public Bitmap gBitmap;
    public GameDisplay(Context context,int screenWidth,int screenHeight) {
        super(context);
        gHolder=this.getHolder();
        gHolder.addCallback(this);
        gHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        gSurfaceTexture=new SurfaceTexture(MAGIC_TEXTURE_ID);
        this.screenWidth=screenWidth;
        this.screenHeight=screenHeight;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        Log.v(TAG, "GameDisplay surfaceChanged");
        parameters = gCamera.getParameters();    
        List<Size> preSize = parameters.getSupportedPreviewSizes();
        previewWidth = preSize.get(0).width;
        previewHeight = preSize.get(0).height;
        for (int i = 1; i < preSize.size(); i++) {
            double similarity = Math
                    .abs(((double) preSize.get(i).height / screenHeight)
                            - ((double) preSize.get(i).width / screenWidth));
            if (similarity >Math.abs(((double) previewHeight / screenHeight)
                                    - ((double) previewWidth / screenWidth))) {
                previewWidth = preSize.get(i).width;
                previewHeight = preSize.get(i).height;
            }
        }
      
        gBitmap= Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
        parameters.setPreviewSize(previewWidth, previewHeight);
        gCamera.setParameters(parameters);
        bufferSize = previewWidth * previewHeight;
        textureBuffer=new int[bufferSize];
        bufferSize  = bufferSize * ImageFormat.getBitsPerPixel(parameters.getPreviewFormat()) / 8;
        gBuffer = new byte[bufferSize];
        gCamera.addCallbackBuffer(gBuffer);
        gCamera.setPreviewCallbackWithBuffer(this);
        gCamera.startPreview();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.v(TAG, "GameDisplay surfaceCreated");
        if (gCamera == null) {
            gCamera = Camera.open(0);
        }
        try {
			gCamera.setPreviewTexture(gSurfaceTexture);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.v(TAG, "GameDisplay surfaceDestroyed");
        gCamera.stopPreview();
        gCamera.release();
        gCamera=null;
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        Log.v(TAG, "GameDisplay onPreviewFrame");
        camera.addCallbackBuffer(gBuffer);  //<----这句一点要加上.
     
        if(gCamera==null){   //if after release ,here also called; so do if.
        	return ;
        }
        synchronized (gHolder)
        {        
		        int w = camera.getParameters().getPreviewSize().width;  
		        int h = camera.getParameters().getPreviewSize().height;
		        int[] rgb = decodeYUV420SP(data, w, h);
		        Canvas canvas = this.getHolder().lockCanvas();  
		        canvas.drawBitmap(rgb, 0, w, 0, 0, w, h, false, null);
		        this.getHolder().unlockCanvasAndPost(canvas);  
        }
    }
    public int[] decodeYUV420SP(byte[] yuv420sp, int width, int height) {  
    	  
        final int frameSize = width * height;  
  
        for (int j = 0, yp = 0; j < height; j++) {  
            int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;  
            for (int i = 0; i < width; i++, yp++) {  
                int y = (0xff & ((int) yuv420sp[yp])) - 16;  
                if (y < 0) y = 0;  
                if ((i & 1) == 0) {  
                    v = (0xff & yuv420sp[uvp++]) - 128;  
                    u = (0xff & yuv420sp[uvp++]) - 128;  
                }  
  
                int y1192 = 1192 * y;  
                int r = (y1192 + 1634 * v);  
                int g = (y1192 - 833 * v - 400 * u);  
                int b = (y1192 + 2066 * u);  
  
                if (r < 0) r = 0;  
                else if (r > 262143) r = 262143;  
                if (g < 0) g = 0;  
                else if (g > 262143) g = 262143;  
                if (b < 0) b = 0;  
                else if (b > 262143) b = 262143;  
  
                textureBuffer[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) &  
                    0xff00) | ((b >> 10) & 0xff);  
  
            }  
        }  
        return textureBuffer;  
    }  
}
package com.example.testsurfacetexture;

import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.SurfaceView;
import android.widget.FrameLayout;

public class MainActivity extends Activity {
	private GameDisplay gameDisplay;
	int screenWidth,screenHeight;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
//		gameDisplay.setVisibility(SurfaceView.VISIBLE);
		DisplayMetrics dm = getResources().getDisplayMetrics();
		screenWidth = dm.widthPixels;
		screenHeight = dm.heightPixels;
		gameDisplay= new GameDisplay(this,dm.widthPixels,dm.heightPixels);
		//加入到当前activity的layout中
		FrameLayout root = (FrameLayout) findViewById(R.id.root);
		root.addView(gameDisplay,0);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

参考过的有:

http://phenom.iteye.com/blog/1170616

http://www.cnblogs.com/hrlnw/p/3277300.html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明
SurfaceTexture 是 Android 中提供的一个类,用于将实时相机预览数据渲染到一个 OpenGL ES 纹理上,以便进行后续的图像处理。实现相机预览的一般流程如下: 1. 打开相机,设置相机参数; 2. 创建一个 SurfaceTexture 对象,并将其设置为相机预览回调中的 Surface; 3. 在 SurfaceTexture 的 onFrameAvailable 回调中,获取最新的相机预览帧数据,并将其渲染到一个 OpenGL ES 纹理上; 4. 在 OpenGL ES 纹理上进行后续的图像处理。 以下是一个简单的示例代码: ```java // 打开相机 Camera camera = Camera.open(); // 设置相机参数 Camera.Parameters parameters = camera.getParameters(); parameters.setPreviewSize(width, height); camera.setParameters(parameters); // 创建一个 SurfaceTexture 对象 SurfaceTexture surfaceTexture = new SurfaceTexture(0); surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() { @Override public void onFrameAvailable(SurfaceTexture surfaceTexture) { // 获取最新的相机预览帧数据,并将其渲染到一个 OpenGL ES 纹理上 surfaceTexture.updateTexImage(); // 在 OpenGL ES 纹理上进行后续的图像处理 // ... } }); // 将 SurfaceTexture 设置为相机预览回调中的 Surface camera.setPreviewTexture(surfaceTexture); // 开始相机预览 camera.startPreview(); ``` 需要注意的是,在渲染相机预览数据之前,需要先创建一个 OpenGL ES 纹理,可以使用 GLES20 类中的 glGenTextures 方法来生成一个纹理 ID。在渲染时,可以使用 SurfaceTexture 的 getTransformMatrix 方法获取纹理坐标变换矩阵,以便正确地将相机预览数据渲染到纹理上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值