![](/img/trans.png)
[英]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.