我使用的SurfaceView 为camera显示图片,
其中代码:
定义了
SurfaceView sView;
ISurfaceHolder surfaceHolder;
OnCreate里面:
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); RequestWindowFeature(WindowFeatures.NoTitle); Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen); SetContentView(Resource.Layout.layoutCameraMain); // Create your application here ////获取ip地址 ipname = this.Intent.GetStringExtra("ipname"); //获取屏幕分辨率 DisplayMetrics dm = new DisplayMetrics(); Display display = WindowManager.DefaultDisplay; display.GetMetrics(dm); //screenWidth = dm.WidthPixels; //screenHeight = dm.HeightPixels; screenWidth = display.Width; screenHeight = display.Height; sView = FindViewById<SurfaceView>(Resource.Id.sView);//获取界面中的surface surfaceHolder = sView.Holder;//获得SurfaceView的SurfaceHolder; ISurfaceHolderCallback ish = new SurfaceHolderCallBack(this); surfaceHolder.AddCallback(ish); // 设置该SurfaceView自己不维护缓冲 surfaceHolder.SetType(SurfaceType.PushBuffers); }
自己实现定义的类:
private void InintCamera() { if (!isPreview) { camera = Camera.Open(); } if (camera != null && !isPreview) { try { Camera.Parameters parameters = camera.GetParameters(); parameters.SetPreviewSize(screenWidth, screenHeight);//设置大小 //parameters.setPreviewFpsRange(20, 30); // 每秒显示20~30帧 parameters.PictureFormat = Android.Graphics.ImageFormatType.Nv21;/// 设置图片格式 parameters.SetPictureSize(screenWidth, screenHeight); //parameters.Set("orientation", "portrait"); //parameters.SetRotation(90);//旋转90度 parameters.PreviewFrameRate = 30; // 每秒显示30帧 camera.SetPreviewDisplay(surfaceHolder); // 通过SurfaceView显示取景画 camera.SetDisplayOrientation(90);//旋转90度 //camera.SetPreviewCallback(new StreamIt(ipname, camera)); camera.SetParameters(parameters); camera.StartPreview(); // 开始预览 camera.AutoFocus(null); // 自动对焦 } catch (Exception e) { string error = e.StackTrace; } } } class MyThread : Java.Lang.Thread { } /// <summary> /// 试映回掉 /// </summary> class StreamIt : Camera.IPreviewCallback { string ipname; Camera camera; public StreamIt(string ipname, Camera camera) : base() { this.camera = camera; this.ipname = ipname; } public void OnPreviewFrame(byte[] data, Camera camera) { Camera.Size size = camera.GetParameters().PreviewSize; try { //调用image.compressToJpeg()将YUV格式图像数据data转为jpg格式 Android.Graphics.YuvImage image = new Android.Graphics.YuvImage(data, Android.Graphics.ImageFormatType.Nv21, size.Width, size.Height, null); if (image != null) { MemoryStream mstream = new MemoryStream(); image.CompressToJpeg(new Android.Graphics.Rect(0, 0, size.Width, size.Height), 80, mstream); mstream.Flush(); //启用线程把图片发送出去 } } catch (Exception ex) { Log.Error("转换图片时出错", ex.Message); } //throw new NotImplementedException(); } public void Dispose() { throw new NotImplementedException(); } public IntPtr Handle { get { return camera.Handle; } } } /// <summary> /// 定义SurfaceHolder回掉 /// </summary> public class SurfaceHolderCallBack : ISurfaceHolderCallback { ActivityCameraMain _main; public SurfaceHolderCallBack(ActivityCameraMain main) : base() { _main = main; } public void SurfaceChanged(ISurfaceHolder holder, Android.Graphics.Format format, int width, int height) { //throw new NotImplementedException(); } public void SurfaceCreated(ISurfaceHolder holder) { //throw new NotImplementedException(); _main.InintCamera(); } public void SurfaceDestroyed(ISurfaceHolder holder) { //throw new NotImplementedException(); if (_main.camera != null) { if (_main.isPreview) { _main.camera.StopPreview(); _main.camera.Release(); _main.camera = null; } //System.exit(0); } } void IDisposable.Dispose() { ; } IntPtr IJavaObject.Handle { get { return ((View)_main.sView).Handle; } } }
似乎是在
IntPtr IJavaObject.Handle { get { return ((View)_main.sView).Handle; } }
出错了,但是没有确定找到是哪里出错了。。
求指教。。
这个Mono啊, 搞android,还是Java好,用这个开发的人应该不多,但愿有人会啊
额 想试着用他写个小程序呢 各种出错。。。
已经找到错误原因了 这么长时间没有结贴。。
所在的Activity继承自要使用到的那个接口就好了