简体   繁体   English

如何在 xamarin.forms 中将流转换为位图以调整图像对比度

[英]How to convert stream to bitmap in xamarin.forms to adjust image contrast

I am trying to raise the contrast of an image in Xamarin.forms using this algorithm.我正在尝试使用算法提高 Xamarin.forms 中图像的对比度。 My code gives me in image in the form of a System.IO.Stream, which I want to convert to a Bitmap/LockBitmap to use the algorithm.我的代码以 System.IO.Stream 的形式提供图像,我想将其转换为 Bitmap/LockBitmap 以使用该算法。

I tried code such as using System.Drawing , which didn't work.我尝试过using System.Drawing代码,但没有用。 The error said "the type or namespace bitmap could not be found."错误提示“找不到类型或命名空间位图”。 Then, I tried using Android.Graphics , which fixes that error, but I don't know if it will work with a Xamarin.forms app that needs to run on Android and iOS.然后,我尝试using Android.Graphics ,它修复了该错误,但我不知道它是否适用于需要在 Android 和 iOS 上运行的 Xamarin.forms 应用程序。

Even if that does work, how do I actually convert a System.IO.Stream into a Bitmap?即使这确实有效,我如何实际将 System.IO.Stream 转换为位图? Is there another algorithm that doesn't require bitmaps?是否有另一种不需要位图的算法?

Edit: I think I can use BitmapFactory to convert the stream into a bitmap.编辑:我想我可以使用 BitmapFactory 将流转换为位图。

you could use DependencyService to convert System.IO.Stream into Bitmap ,after raise the contrast of an image ,return the new stream,and show the Image in forms page.您可以使用DependencyServiceSystem.IO.Stream转换为Bitmap ,在提高图像对比度后,返回新流,并在表单页面中显示Image

like:喜欢:

create a interface IRaiseImage.cs :创建一个接口IRaiseImage.cs

public interface IRaiseImage
{
    Stream RaiseImage(Stream stream);
}

then in Droid.project,creat AndroidRaiseImage.cs :然后在 Droid.project 中,创建AndroidRaiseImage.cs

[assembly: Dependency(typeof(AndroidRaiseImage))]
namespace App18.Droid
{
  class AndroidRaiseImage : IRaiseImage
   {
     public Stream RaiseImage(Stream stream)
      {
        Bitmap bitmap = BitmapFactory.DecodeStream(stream);
        //raise the bitmap contrast
        ...
        // return the new stream
        MemoryStream ms = new MemoryStream();
        bitmap.Compress(Bitmap.CompressFormat.Png, 100, ms);
        return ms;
      }
   }
}

then you could set to the Image in your forms page:那么您可以在表单页面中设置为图像:

MemoryStream memoryStream = new MemoryStream();//your original image stream
Image image = new Image();
image.Source = ImageSource.FromStream(() => DependencyService.Get<IRaiseImage>().RaiseImage(memoryStream));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM