Android摄像头视频帧数据测试代码

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PreviewCallback;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity implements SurfaceHolder.Callback,PreviewCallback{
	
	private Camera camera;
	private TextView tv;
	private SurfaceView sv ;
	private boolean flag = false;
	private int cnt = 0;
	public void onSwitch(View v){
		if(flag){
			flag = false;
			LayoutParams params = sv.getLayoutParams();
			params.width = 600;
			params.height = 600;
			sv.setLayoutParams(params);
		}else{
			flag = true;
			LayoutParams params = sv.getLayoutParams();
			params.width = 1;
			params.height = 1;
			sv.setLayoutParams(params);
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv = (TextView)findViewById(R.id.tv_id);
		sv = (SurfaceView) findViewById(R.id.surface_view);
        sv.getHolder().addCallback(this);
	}
	
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		
	}
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		try{
			camera = Camera.open();
			camera.setPreviewDisplay(holder);
			Parameters params = camera.getParameters();
			params.setPreviewSize(352, 288);
			camera.setDisplayOrientation(90);
			camera.setParameters(params);
			camera.startPreview() ;
			camera.setPreviewCallback(this);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if(camera != null){
			camera.setPreviewCallback(null);
			camera.stopPreview();
			camera.release();
			camera = null;
		}
	}
	@Override
	public void onPreviewFrame(byte[] data, Camera camera) {
		tv.setText("屏幕上没有预览视频,也有视频数据回调 \n\r Frame data come cnt="+ cnt++);
	}
}

布局文件:




	
	

	
权限设置:

	
	
	
	



你可能感兴趣的:(Android)