[英]Can the Xamarin Dependency service work if the caller is not using Xamarin.Forms?
[英]Compress size of image to 250kb using xamarin.forms without dependency service
我正在尝试将 Xamarin.Forms 中从相机拍摄的图像压缩为 250kb 大小。 我找到了在依赖服务中做到这一点的方法,但我希望它没有依赖服务(纯 xamarin.forms 代码)。 怎么可能。 任何人都可以建议我最好的方法吗?
提前致谢
这是一项非常复杂的工作,因为您需要大量有关图像处理的知识。
最重要的是,重新发明轮子是一个糟糕的举动。
http://www.codeproject.com/Articles/83225/A-Simple-JPEG-Encoder-in-C
看一下上面只处理JPEG的代码项目; 更不要说 TIFF、GIF、BMP 等。
图像压缩涉及许多复杂的数学变换,如 DCT 和 Huffman。
你需要一整个大学学期来学习这些基础知识。
另一方面,明智地利用平台支持,您可以在一分钟内完成任务。
Windows Phone 中的位图编码器。
FileStream stream = new FileStream("new.jpg", FileMode.Create);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.QualityLevel = 30;
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);
Android中的位图
using (System.IO.Stream stream = System.IO.File.Create(targetFile))
{
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 30, stream);
}
iOS 中的UIImage
NSData data = image.AsJPEG(0.3);
.NET 框架中的位图
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
ImageCodecInfo codec = codecs.First(t => t.FormatID == ImageFormat.Jpeg.Guid);
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, 30L);
bitmap.Save("output.jpg", codec, parameters);
我也有同样的问题。 请在此处查看,因为我相信您会找到解决方案。
https://xamarincodes.com/2020/04/05/image-compression-in-xamarin-forms/
我使用 Xam.Plugin.Media – 设置压缩质量来拍照和压缩。
这是一个示例
private async void cmdCameraPhotograph_Clicked(object sender, EventArgs e)
{
if (CrossMedia.Current.IsTakePhotoSupported)
{
var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
Directory = "Photographs",
SaveToAlbum = true,
CompressionQuality = 40,
CustomPhotoSize = 35,
PhotoSize = PhotoSize.MaxWidthHeight,
MaxWidthHeight = 2000,
DefaultCamera = CameraDevice.Rear
}).ConfigureAwait(true);
if (file != null)
{
}
}
else
{
await DisplayAlert("Not Supported", "Your device does not support this feature.", "OK. Understood")
.ConfigureAwait(true);
}
}
您也可以从图库中获取文件
var file = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions
{
CompressionQuality = 40,
CustomPhotoSize = 35,
PhotoSize = PhotoSize.MaxWidthHeight,
MaxWidthHeight = 2000
}).ConfigureAwait(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.