本文介绍了使用没有依赖服务的 xamarin.forms 将图像大小压缩到 250kb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Xamarin.Forms 中从相机拍摄的图像压缩为 250kb 大小.我找到了在依赖服务中做到这一点的方法,但我希望它没有依赖服务(纯 xamarin.forms 代码).怎么可能.谁能建议我最好的方法?

I'm trying to compress image taken from camera to 250kb size in Xamarin.Forms. I found ways to do that in dependency service but I want it without dependency service (pure xamarin.forms code). How could it possible. Can anyone suggest me best approaches you have?

提前致谢

推荐答案

这是一项非常复杂的工作,因为您需要大量有关图像处理的知识.

It is a very complicated job since you would need a ton of knowledge about image processing.

最重要的是,重新发明轮子是一个糟糕的举动.

Most importantly, re-inventing wheel is a bad move.

http://www.codeproject.com/文章/83225/A-Simple-JPEG-Encoder-in-C

看看上面只处理JPEG的代码项目;更别说 TIFF、GIF、BMP 等.

Take a look of the above code project which only tackles JPEG; not to say TIFF, GIF, BMP etc.

图像压缩涉及许多复杂的数学变换,如 DCT 和 Huffman.

Image compression involves many complex mathematics transforms, like DCT and Huffman.

你需要一整个大学学期来学习这些基础知识.

You will need a whole university semester to learn those basics.

另一方面,明智地利用平台支持,您可以在一分钟内完成任务.

On the other hand, wisely utilizing platform support, you can complete the task within a minute.

BitmapEncoder 在 Windows Phone 中.

BitmapEncoder in 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);

位图在安卓中

using (System.IO.Stream stream = System.IO.File.Create(targetFile))
{
    bitmap.Compress(Bitmap.CompressFormat.Jpeg, 30, stream);
}

UIImage 在 iOS 中

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);

这篇关于使用没有依赖服务的 xamarin.forms 将图像大小压缩到 250kb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:31