ZXing 如何设置位图的大小?
How to set size of the bitmap in ZXing?
我正在使用ZXing生成二维码。这是我的代码的样子:
public partial class QRPage : ContentPage
{
public QRPage()
{
InitializeComponent();
var stream = DependencyService.Get<IBarcodeService>().ConvertImageStream("nika");
qrImage.Source = ImageSource.FromStream(() => { return stream; });
qrImage.HeightRequest = 200;
}
}
而另一部分:
[assembly: Xamarin.Forms.Dependency(typeof(BarcodeService))]
namespace MyApp.Droid
{
public class BarcodeService : IBarcodeService
{
public Stream ConvertImageStream(string text, int width = 500, int height = 500)
{
var barcodeWriter = new ZXing.Mobile.BarcodeWriter
{
Format = ZXing.BarcodeFormat.QR_CODE,
Options = new ZXing.Common.EncodingOptions
{
Width = width,
Height = height,
Margin = 2
}
};
barcodeWriter.Renderer = new ZXing.Mobile.BitmapRenderer();
var bitmap = barcodeWriter.Write(text);
var stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
stream.Position = 0;
return stream;
}
}
}
这是我使用代码的 xaml:
<StackLayout Padding="20,30,20,30">
<Label Text="..." FontSize="Medium "/>
<Frame VerticalOptions="CenterAndExpand">
<Image x:Name="qrImage" WidthRequest="300" />
</Frame>
...
</StackLayout>
问题是,无论我为 ConvertImageStream
设置什么高度和宽度,生成的图像都不是正方形,而是如下所示:
如何把它变成正方形?提前致谢。
您的代码正确且图像大小正确:
我通过保存进行了双重检查:
var filePath = System.IO.Path.Combine(Environment.ExternalStorageDirectory.AbsolutePath, "qrcode.png");
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
bitmap.Compress(Bitmap.CompressFormat.Png, 100, fileStream);
}
然后拉动它并检查它的大小:
$ adb pull /sdcard/qrcode.png
[100%] /sdcard/qrcode.png
$ identify qrcode.png
qrcode.png PNG 500x500 500x500+0+0 8-bit sRGB 2.85KB 0.000u 0:00.000
我的页面代码:
var image = new Image();
var content = new ContentPage
{
BackgroundColor = Color.Red,
Title = "ZXingQRCode",
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
HorizontalTextAlignment = TextAlignment.Center,
Text = "Whosebug",
FontSize = 40
},
image
}
}
};
MainPage = new NavigationPage(content);
var stream = DependencyService.Get<IBarcodeService>().ConvertImageStream("https://whosebug.com");
image.Source = ImageSource.FromStream(() => { return stream; });
image.HeightRequest = 250;
更新:
您看到的 Frame
比图像宽,而不是图像本身。更改Frame
的BackgroundColor
<StackLayout Padding="20,30,20,30">
<Label Text="..." FontSize="Medium" />
<Frame VerticalOptions="CenterAndExpand" BackgroundColor="Red">
<Image x:Name="qrImage" WidthRequest="300" />
</Frame>
</StackLayout>
我正在使用ZXing生成二维码。这是我的代码的样子:
public partial class QRPage : ContentPage
{
public QRPage()
{
InitializeComponent();
var stream = DependencyService.Get<IBarcodeService>().ConvertImageStream("nika");
qrImage.Source = ImageSource.FromStream(() => { return stream; });
qrImage.HeightRequest = 200;
}
}
而另一部分:
[assembly: Xamarin.Forms.Dependency(typeof(BarcodeService))]
namespace MyApp.Droid
{
public class BarcodeService : IBarcodeService
{
public Stream ConvertImageStream(string text, int width = 500, int height = 500)
{
var barcodeWriter = new ZXing.Mobile.BarcodeWriter
{
Format = ZXing.BarcodeFormat.QR_CODE,
Options = new ZXing.Common.EncodingOptions
{
Width = width,
Height = height,
Margin = 2
}
};
barcodeWriter.Renderer = new ZXing.Mobile.BitmapRenderer();
var bitmap = barcodeWriter.Write(text);
var stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
stream.Position = 0;
return stream;
}
}
}
这是我使用代码的 xaml:
<StackLayout Padding="20,30,20,30">
<Label Text="..." FontSize="Medium "/>
<Frame VerticalOptions="CenterAndExpand">
<Image x:Name="qrImage" WidthRequest="300" />
</Frame>
...
</StackLayout>
问题是,无论我为 ConvertImageStream
设置什么高度和宽度,生成的图像都不是正方形,而是如下所示:
如何把它变成正方形?提前致谢。
您的代码正确且图像大小正确:
我通过保存进行了双重检查:
var filePath = System.IO.Path.Combine(Environment.ExternalStorageDirectory.AbsolutePath, "qrcode.png");
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
bitmap.Compress(Bitmap.CompressFormat.Png, 100, fileStream);
}
然后拉动它并检查它的大小:
$ adb pull /sdcard/qrcode.png
[100%] /sdcard/qrcode.png
$ identify qrcode.png
qrcode.png PNG 500x500 500x500+0+0 8-bit sRGB 2.85KB 0.000u 0:00.000
我的页面代码:
var image = new Image();
var content = new ContentPage
{
BackgroundColor = Color.Red,
Title = "ZXingQRCode",
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
HorizontalTextAlignment = TextAlignment.Center,
Text = "Whosebug",
FontSize = 40
},
image
}
}
};
MainPage = new NavigationPage(content);
var stream = DependencyService.Get<IBarcodeService>().ConvertImageStream("https://whosebug.com");
image.Source = ImageSource.FromStream(() => { return stream; });
image.HeightRequest = 250;
更新:
您看到的 Frame
比图像宽,而不是图像本身。更改Frame
BackgroundColor
<StackLayout Padding="20,30,20,30">
<Label Text="..." FontSize="Medium" />
<Frame VerticalOptions="CenterAndExpand" BackgroundColor="Red">
<Image x:Name="qrImage" WidthRequest="300" />
</Frame>
</StackLayout>