大佬教程收集整理的这篇文章主要介绍了xamarin.forms – Xamarin表单 – 调整相机图片大小,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
picker = Dependencyservice.Get<IMediaPicker> (); task = picker.TakePhotoAsync (new CameraMediaStorageOptions { DefaultCamera = CameraDevice.Rear,MaxPixelDimension = 800,}); img.BACkgroundColor = Color.Gray; Device.StartTimer (TimeSpan.FromMilliseconds (250),() => { if (task != null) { if (task.Status == TaskStatus.RanToCompletion) { Device.beginInvokeOnMainThread (async () => { //img.source = Imagesource.FromStream (() => task.Result.sourcE); var fileAccess = Resolver.Resolve<IFileAccess> (); String imagename = "img_user_" + User.CurrentUser().id + "_" + datetiR_228_11845@e.Now.ToString ("yy_MM_dd_HH_mm_ss") + ".jpg"; filename = imagename; fileAccess.WriteStream (imagename,task.Result.sourcE); fileLOCATIOn = fileAccess.FullPath(imageName); FileStream fileStream = new FileStream(fileAccess.FullPath(imageName),FileMode.open,System.IO.FileAccess.Read); imageUrl = (String)test[0]["url"]; img.source = imageUrl; }); } return task.Status != TaskStatus.Canceled && task.Status != TaskStatus.Faulted && task.Status != TaskStatus.RanToCompletion; } return true; });
它可以保存图像,但手机拍照的实际尺寸很大,有没有办法调整尺寸.
原始答案(不要使用):
我发现了一个图像I / O库,ImageTools-PCL,它是我forked on github并修剪了在Xamarin中无法编译的内容,将修改保持在最低限度并且结果似乎有效.
要使用它,请下载链接的存储库,使用Xamarin进行编译,并将Build文件夹中的DLL添加到Forms项目中.
要调整图像大小,您可以执行此操作(应该适合您的问题的上下文)
var decoder = new ImageTools.IO.Jpeg.JpegDecoder (); ImageTools.ExtendedImage inImage = new ImageTools.ExtendedImage (); decoder.Decode (inImage,task.Result.sourcE); var outIR_228_11845@age = ImageTools.ExtendedImage.Resize (inImage,1024,new ImageTools.Filtering.bilinearResizer ()); var encoder = new ImageTools.IO.Jpeg.JpegEncoder (); encoder.Encode (outIR_228_11845@age,fileAccess.CreateStream (imageName)); Imagesource imgsource = Imagesource.FromFile (fileAccess.FullPath (imageName));
更新的答案:
从nuget获取Xamarin.XLabs,learn about using Resolver,使用Resize方法创建一个IImageservice接口.
iOS的实现:
public class ImageserviceIOS: IImageservice{ public void ResizeImage(String sourceFile,String targetFile,float maxWidth,float maxHeight) { if (File.Exists(sourceFilE) && !File.Exists(targetFilE)) { using (UIImage sourceImage = UIImage.FromFile(sourceFilE)) { var sourceSize = sourceImage.Size; var maxResizeFactor = Math.Min(maxWidth / sourceSize.Width,maxHeight / sourceSize.Height); if (!Directory.Exists(Path.GetDirectoryName(targetFilE))) Directory.CreateDirectory(Path.GetDirectoryName(targetFilE)); if (maxResizeFactor > 0.9) { File.Copy(sourceFile,targetFilE); } else { var width = maxResizeFactor * sourceSize.Width; var height = maxResizeFactor * sourceSize.Height; UIGraphics.beginImageContextWithOptions(new CGSize((float)width,(float)height),true,1.0f); // UIGraphics.GetCurrentContext().RotateCTM(90 / Math.PI); sourceImage.Draw(new CGRect(0,(float)width,(float)height)); var resultImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); if (targetFile.ToLower().EndsWith("png")) resultImage.AsPNG().Save(targetFile,truE); else resultImage.AsJPEG().Save(targetFile,truE); } } } } }
Android服务的实施:
public class ImageserviceDroid: IImageservice{ public void ResizeImage(String sourceFile,float maxHeight) { if (!File.Exists(targetFilE) && File.Exists(sourceFilE)) { // First decode with inJustDecodeBounds=true to check dimensions var options = new BitmapFactory.options() { InJustDecodeBounds = false,InPurgeable = true,}; using (var image = BitmapFactory.DecodeFile(sourceFile,options)) { if (image != null) { var sourceSize = new Size((int)image.GetBitmapInfo().Height,(int)image.GetBitmapInfo().Width); var maxResizeFactor = Math.Min(maxWidth / sourceSize.Width,maxHeight / sourceSize.Height); String targetDir = System.IO.Path.GetDirectoryName(targetFilE); if (!Directory.Exists(targetDir)) Directory.CreateDirectory(targetDir); if (maxResizeFactor > 0.9) { File.Copy(sourceFile,targetFilE); } else { var width = (int)(maxResizeFactor * sourceSize.Width); var height = (int)(maxResizeFactor * sourceSize.Height); using (var bitmapScaled = Bitmap.CreateScaledBitmap(image,height,width,truE)) { using (Stream outStream = File.Create(targetFilE)) { if (targetFile.ToLower().EndsWith("png")) bitmapScaled.Compress(Bitmap.CompressFormat.Png,100,outStream); else bitmapScaled.Compress(Bitmap.CompressFormat.Jpeg,95,outStream); } bitmapScaled.Recycle(); } } image.Recycle(); } else Log.E("Image scaling failed: " + sourceFilE); } } } }
以上是大佬教程为你收集整理的xamarin.forms – Xamarin表单 – 调整相机图片大小全部内容,希望文章能够帮你解决xamarin.forms – Xamarin表单 – 调整相机图片大小所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。