关于 setPreviewDisplay(null) 无法正常显示的问题
来源:互联网 发布:易玩游戏盒子网络异常 编辑:程序博客网 时间:2024/06/05 20:02
在 “Android NDK Beginner Guide”中第4章有段例程展示在Java中调用JNI的C代码,使用了CameraView类演示了用JNI C代码自己转换YUV420到RGB888的过程,但是实际这段代码在模拟器上倒是没问题,在很多实机上却无法显示。
在国外网站上一般就说是 Android3.0后的版本中CAmera对象的setPreviewDisplay不能使用null作为参数,否则 onPreviewFrame 回调函数不会被调用。按这个提示修改后,将在界面上看到两个预览图案,类似画中画效果,小的画面是通过 onPreviewFrame -> OnDraw调用的。大的是setPreviewDisplay绑定当前SurfaceView自动刷新的。不知道大家还有没有更好的解决办法。
public class CameraView extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback{static {System.loadLibrary("livaCamera");}public static final String TAG = "CameraView";public native void decode(Bitmap pTarget, byte[] pSource);private Camera mCamera;private byte[] mVideoSource;private Bitmap mBackBuffer;private Paint mPaint;public CameraView(Context context) {super(context);getHolder().addCallback(this);setWillNotDraw(false);}public void surfaceCreated(SurfaceHolder holder) {Log.v(TAG, "surfaceCreated()");try {mCamera = Camera.open();mCamera.setDisplayOrientation(0);// mCamera.setPreviewDisplay(null); // 使用空的SurfaceHolder参数无法显示mCamera.setPreviewDisplay(this.getHolder()); //nullmCamera.setPreviewCallbackWithBuffer(this);Log.d(TAG, "surfaceCreated() startPreview Success "); } catch (Exception e) {mCamera.release();mCamera = null;throw new IllegalStateException();}}public void surfaceChanged(SurfaceHolder pHolder, int pFormat,int pWidth,int pHeight) {Log.v(TAG, "surfaceChanged()");try {mCamera.stopPreview();Log.d(TAG, "surfaceChanged() stopPreview Success ");} catch (Exception e){// ignore: tried to stop a non-existent previewLog.d(TAG, "surfaceChanged() stopPreview Failed ");}Size lSize = findBestResolution(pWidth, pHeight);PixelFormat lPixelFormat = new PixelFormat();PixelFormat.getPixelFormatInfo(mCamera.getParameters().getPreviewFormat(),lPixelFormat);int lSourceSize = lSize.width * lSize.height * lPixelFormat.bitsPerPixel / 8;if( mVideoSource == null ){mVideoSource = new byte[lSourceSize];}if( mBackBuffer == null ){mBackBuffer = Bitmap.createBitmap(lSize.width, lSize.height,Bitmap.Config.ARGB_8888);}Camera.Parameters lParameters = mCamera.getParameters();lParameters.setPreviewSize(lSize.width, lSize.height);lParameters.setPreviewFormat(ImageFormat.NV21); // PixelFormat.YCbCr_422_I deprecated in API level 8.mCamera.setParameters(lParameters);mCamera.addCallbackBuffer(mVideoSource); try { //mCamera.setPreviewDisplay(null); mCamera.startPreview(); Log.d(TAG, "surfaceChanged() startPreview Success "); } catch (Exception e){ // ignore: tried to stop a non-existent preview Log.d(TAG, "surfaceChanged() startPreview Failed "); } }private Size findBestResolution(int pWidth, int pHeight){List<Size> lSizes = mCamera.getParameters().getSupportedPreviewSizes();Size lSelectedSize = mCamera.new Size(0, 0);for (Size lSize : lSizes) {if ((lSize.width <= pWidth)&& (lSize.height <= pHeight)&& (lSize.width >= lSelectedSize.width)&& (lSize.height >= lSelectedSize.height)) {lSelectedSize = lSize;}}if ((lSelectedSize.width == 0)|| (lSelectedSize.height == 0)) {lSelectedSize = lSizes.get(0);}return lSelectedSize;}public void surfaceDestroyed(SurfaceHolder holder) {Log.v(TAG, "surfaceDestroyed()");if (mCamera != null) {mCamera.stopPreview();try {mCamera.reconnect();} catch (IOException e) {e.printStackTrace();}mCamera.release();mCamera = null;mVideoSource = null;mBackBuffer = null;}}public void onPreviewFrame(byte[] pData, Camera pCamera){int[] px = new int[100];Log.v(TAG, "onPreviewFrame()");decode(mBackBuffer, pData);mFrameCnt++;invalidate();}@Overrideprotected void onDraw(Canvas pCanvas){Log.v(TAG, "onDraw()");if (mCamera != null) {pCanvas.drawBitmap(mBackBuffer, 0, 0, mPaint);mCamera.addCallbackBuffer(mVideoSource);}}}注意
- 关于 setPreviewDisplay(null) 无法正常显示的问题
- 关于开机显示GRUB 无法正常进入系统的问题
- IE无法正常显示的问题
- activity注册的问题,无法正常显示
- webview无法正常显示图片的问题
- 关于使用Adoeb SVG Viewer在IE中无法正常显示SVG文档的问题
- 关于jquery操作iframe中内容, 无法正常显示的问题
- 关于OA系统ie8上传附件无法正常显示的问题
- 关于Vuforia云识别的粒子特效无法正常显示问题
- ubuntu console 中文无法正常显示的问题
- 某些.chm格式文件无法正常显示的问题
- CentOS/Linux+SecureCRT 无法正常显示/乱码问题的解决方法
- JasperReports 6.0.0中文字体无法正常显示的问题
- PIL库无法正常显示图片的问题
- 网页中DIV无法正常显示的问题
- 使用bootstrapValidator无法正常显示提示信息的问题
- Linux连接投影仪 (解决无法正常显示的问题)
- ASP.Net 解决SiteMapPath无法正常显示的问题
- ibatis各个版本下载链接
- szieof
- FastDFS一个高效的分布式文件系统
- Q7--两倍问题
- FastDFS HOWTO - 文件及目录结构
- 关于 setPreviewDisplay(null) 无法正常显示的问题
- 广州集体户口办理未婚证流程
- linux学习入门5——linux系統常用命令(linuxcast.net)(日期、文件查看(head,tail等)、查看硬件信息、关机重启、归档压缩及查找命令)
- FastDFS HOWTO - 同步机制
- #define
- 自定义视图(二)
- 外媒评十大创新游戏公司
- 黑马程序员——高新技术(day1)增强for循环,枚举
- linux内核设备模型中的总线