/// <summary> /// 开始截屏 /// </summary> /// <param name="imageFileDateTime">时间参数 - 屏幕截图文件名命名规则所需</param> /// <param name="dirName">屏幕截图保存文件夹名称</param> public System.IO.FileInfo OnScreenshot(DateTime?imageFileDateTime, string dirName = "") { if (s_IsRunning.Get() == true) { string msg = "正在进行截屏, 无法再多开一个"; System.Diagnostics.Debug.WriteLine(msg); showToast(msg); return(null); } s_IsRunning.Set(true); mImageFileDateTime = imageFileDateTime; mDirName = dirName; Android.Content.Intent intent = mProjectionManager.CreateScreenCaptureIntent(); mAppActivity.StartActivityForResult(intent, s_Screenshot_Request_Code); return(MyAndroidScreenshot.GetScreenshotFileInfo(mImageFileDateTime, mDirName)); }
public void OnImageAvailable(Android.Media.ImageReader imageReader) { Android.Media.Image image = null; Android.Graphics.Bitmap bitmapFromPlane0 = null; Android.Graphics.Bitmap bitmapToSave = null; try { image = imageReader.AcquireLatestImage(); if (image != null) { Android.Media.Image.Plane[] planes = image.GetPlanes(); if (planes.Length > 0) { int rowPadding = planes[0].RowStride - planes[0].PixelStride * mWidth; bitmapFromPlane0 = Android.Graphics.Bitmap.CreateBitmap ( width: (mWidth + rowPadding / planes[0].PixelStride), // 此处不能直接采用 Width, 否则保存的图片会出现花屏 height: mHeight, config: Android.Graphics.Bitmap.Config.Argb8888 ); bitmapFromPlane0.CopyPixelsFromBuffer(planes[0].Buffer); var imagefileInfo = MyAndroidScreenshot.GetScreenshotFileInfo(mImageFileDateTime, mDirName); // 传入目录名称 if (imagefileInfo.Directory.Exists == false) { imagefileInfo.Directory.Create(); } string imageFile = imagefileInfo.FullName; // bitmapFromPlane0 多增加的长度减至 屏幕的 Width bitmapToSave = Android.Graphics.Bitmap.CreateBitmap(bitmapFromPlane0, 0, 0, mWidth, mHeight); using (System.IO.FileStream stream = new System.IO.FileStream(path: imageFile, System.IO.FileMode.CreateNew)) { bitmapToSave.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 60, stream); // 压缩图片 // TODO 控制图片质量 stream.Flush(); } // 发送广播 Android.App.Application.Context.SendBroadcast ( new Android.Content.Intent ( action: Android.Content.Intent.ActionMediaScannerScanFile, uri: Android.Net.Uri.FromFile(new Java.IO.File(imageFile)) ) ); System.Diagnostics.Debug.WriteLine("MyImageReaderListener-OnImageAvailable - 成功保存屏幕截图"); } } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("MyImageReaderListener-OnImageAvailable - 捕获异常"); System.Diagnostics.Debug.WriteLine(e.GetFullInfo()); System.Diagnostics.Debugger.Break(); } finally { if (bitmapFromPlane0 != null) { bitmapFromPlane0.Recycle(); bitmapFromPlane0 = null; } if (image != null) { image.Close(); image = null; } MyAndroidScreenshot.GetInstance().ReleaseAfterScreenCaptureSave(); MyAndroidScreenshot.s_IsRunning.Set(false); } }
public System.IO.FileInfo Get_ScreenshotFileInfo(DateTime?imageFileDateTime = null, string dirName = "") { return(MyAndroidScreenshot.GetScreenshotFileInfo(imageFileDateTime, dirName)); }
public System.IO.FileInfo OnScreenshotFromActivity(DateTime?imageFileDateTime = null, string dirName = "") { mImageFileDateTime = imageFileDateTime; mDirName = dirName; // View view = ctx.getWindow().getDecorView(); var view = mAppActivity.Window.DecorView; // view.setDrawingCacheEnabled(true); view.DrawingCacheEnabled = true; // view.buildDrawingCache(); view.BuildDrawingCache(); Android.Graphics.Bitmap bp = Android.Graphics.Bitmap.CreateBitmap ( view.DrawingCache, 0, 0, view.MeasuredWidth, view.MeasuredHeight ); // view.setDrawingCacheEnabled(false); view.DrawingCacheEnabled = false; // view.destroyDrawingCache(); view.DestroyDrawingCache(); var imagefileInfo = MyAndroidScreenshot.GetScreenshotFileInfo(mImageFileDateTime, mDirName); // 传入目录名称 if (imagefileInfo.Directory.Exists == false) { imagefileInfo.Directory.Create(); } string imageFile = imagefileInfo.FullName; using (System.IO.FileStream stream = new System.IO.FileStream(path: imageFile, System.IO.FileMode.CreateNew)) { bp.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 60, stream); // 压缩图片 // TODO 控制图片质量 stream.Flush(); } // 发送广播 Android.App.Application.Context.SendBroadcast ( new Android.Content.Intent ( action: Android.Content.Intent.ActionMediaScannerScanFile, uri: Android.Net.Uri.FromFile(new Java.IO.File(imageFile)) ) ); string msg = "成功保存屏幕截图"; System.Diagnostics.Debug.WriteLine(msg); showToast(msg); return(imagefileInfo); }