本文介绍了使用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/Articles/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 .

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中的

位图

Bitmap in 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框架中的

位图

Bitmap in .NET framework

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-27 07:29