如何在Android中渲染YV12视频帧

时间:2014-07-29 07:12:44

标签: java android

我有来自MC67移动设备成像器的YV12视频帧。我想在android surface上显示它。到目前为止我无法做到。

我正在使用以下代码。

MainTestActivity.java

public class MainTestActivity extends Activity {
protected static final String TAG = "CameraTestApp";
private Camera mCamera;
private Preview mPreview;
FrameLayout frmLayoutPreview;
Button btnStart;// Test Start button
Button btnRefreshReport ;//Refresh button   
ByteArrayOutputStream out;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   getWindow().setFormat(ImageFormat.YV12);      
    setContentView(R.layout.main);
    mCamera= Camera.open(42);

     //parameters.setPictureFormat(ImageFormat.YV12); 
    mPreview = new Preview(this,mCamera);   
    frmLayoutPreview = (FrameLayout) findViewById(R.id.frmLayoutCameraPreview);
    frmLayoutPreview.addView(mPreview);     

    btnStart = (Button) findViewById(R.id.btnStartTest);        

    btnStart.setOnClickListener(new OnClickListener () {
        public void onClick(View v){
            btnStart.setEnabled(false);
            mCamera.setPreviewCallback(mPreviewCallback);
            mCamera.startPreview();             
            btnRefreshReport.setEnabled(true);
        }
    });   

    btnRefreshReport= (Button) findViewById(R.id.btnRefreshReport);        
    btnRefreshReport.setOnClickListener(new OnClickListener () {
        public void onClick(View v){
            mCamera.stopPreview();
            btnRefreshReport.setEnabled(false);             
            btnStart.setEnabled(true);          
        }
    });        
    btnRefreshReport.setEnabled(false);
    btnStart.setEnabled(true);
}

PreviewCallback mPreviewCallback = new PreviewCallback() {      
    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {

    }
};


@Override
public void onDestroy()
{
    super.onDestroy();
    this.mCamera.release();
}

}

Preview.java

class Preview extends SurfaceView implements SurfaceHolder.Callback {
public SurfaceHolder mHolder;
private Camera mCamera ;
private String TAG="CAMERA_TEST";
List<Integer> supportformats;//new code
String filename= "/sdcard/BB_FPS.txt";

Preview(Context context,Camera objCamera) {
    super(context);
    this.mCamera=objCamera;
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


}

public void surfaceCreated(SurfaceHolder holder) {
    try 
    {   

        mCamera.setPreviewDisplay(holder);

    } 
    catch (Exception e) { 
        Log.i(TAG, "SurfaceCreated error\t"+e.toString());
    }
}

public void surfaceDestroyed(SurfaceHolder holder) 
{
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) 
{
    Log.i(TAG, "SurfaceChanged invoked");       
    if (mHolder.getSurface() == null)
    {            
        return;
    }

    try
    {
        mCamera.stopPreview();

    } 
    catch (Exception e){}

    try 
    {    mCamera.setPreviewDisplay(holder); 

        mCamera.startPreview();

    }
    catch (Exception e)
    {
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }
}   

}

我尝试使用 setPictureFormat(ImageFormat.YV12)方法设置图片格式。但它也不起作用。可以帮助我。

1 个答案:

答案 0 :(得分:0)

我通过使用libyuv库转换为RGB来做同样的事情。但我必须逐帧完成。