ZXingBarcodeImageView Xamarin Forms Zxing 2.3.2 中模糊的 DataMatrix 条码格式

DataMatrix Barcode Format Blurred in ZXingBarcodeImageView Xamarin Forms Zxing 2.3.2

我正在尝试在 ZXingBarcodeImageView 中显示 DataMatrix 条形码,问题是我使用的条形码的宽度和高度都是模糊的低分辨率。

Screenshot

<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
                        <forms1:ZXingBarcodeImageView BarcodeFormat="DATA_MATRIX" BarcodeOptions="{datamatrix:DatamatrixEncodingOptions, Height=100, Width=100}" WidthRequest="100" HeightRequest="100" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BarcodeValue="123456" ></forms1:ZXingBarcodeImageView>

我尝试在 EncodingOptions 和 ZXingBarcodeImageView 本身上设置不同的高度和宽度。

有什么推荐吗?

我遇到了类似的问题。

事实证明,您可以像这样设置 EncodingOptions,例如:

barcodeImageView.BarcodeOptions = new ZXing.Common.EncodingOptions() { Height = 300, Width = 300, PureBarcode = true };

因为生成发生在较低级别并且会按比例放大。

这个答案帮助了我: how to fix an unclear qr code image generated using zxing 2.1?

这个错误只存在于 ZXing portable for DataMatrix 所以 我最终只为 Android 和 iOS 创建了自定义渲染器(Windows 工作正常)。

Android代码:

public class SDataMatrixRenderer : ImageRenderer
{
    public SDataMatrixRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
    {
        base.OnElementChanged(e);
        if (Element == null)
        {
            return;
        }
        Control.SetImageBitmap(GetBitmap(((SDataMatrix)Element).BitMatrix));
    }


    private Bitmap GetBitmap(BitMatrix bitMatrix)
    {
        int BLACK = Color.Black;
        int WHITE = Color.White;

        // change the values to your needs
        int requestedWidth = 200;
        int requestedHeight = 200;

        int width = bitMatrix.Width;
        int height = bitMatrix.Height;

        // calculating the scaling factor
        int pixelsize = requestedWidth / width;
        if (pixelsize > requestedHeight / height)
        {
            pixelsize = requestedHeight / height;
        }

        int[] pixels = new int[requestedWidth * requestedHeight];
        // All are 0, or black, by default
        for (int y = 0; y < height; y++)
        {
            int offset = y * requestedWidth * pixelsize;

            // scaling pixel height
            for (int pixelsizeHeight = 0; pixelsizeHeight < pixelsize; pixelsizeHeight++, offset += requestedWidth)
            {
                for (int x = 0; x < width; x++)
                {
                    int color = bitMatrix[x, y] ? BLACK : WHITE;

                    // scaling pixel width
                    for (int pixelsizeWidth = 0; pixelsizeWidth < pixelsize; pixelsizeWidth++)
                    {
                        pixels[offset + x * pixelsize + pixelsizeWidth] = color;
                    }
                }
            }
        }
        Bitmap bitmap = Bitmap.CreateBitmap(requestedWidth, requestedHeight, Bitmap.Config.Argb8888);
        bitmap.SetPixels(pixels, 0, requestedWidth, 0, 0, requestedWidth, requestedHeight);

        return bitmap;
    }
}

在 iOS:

public class SDataMatrixRenderer : ImageRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
    {
        base.OnElementChanged(e);
        if (Element == null)
        {
            return;
        }
        Control.Image = GetImage(((SDataMatrix)Element).BitMatrix);
    }

    public UIImage GetImage(BitMatrix matrix)
    {
        UIGraphics.BeginImageContext(new CGSize(matrix.Width * 10, matrix.Height * 10));
        CGContext context = UIGraphics.GetCurrentContext();

        CGColor black = new CGColor(0f, 0f, 0f);
        CGColor white = new CGColor(1.0f, 1.0f, 1.0f);
        for (int x = 0; x < matrix.Width; x++)
        {
            for (int y = 0; y < matrix.Height; y++)
            {

                context.SetFillColor(matrix[x, y] ? black : white);
                context.FillRect(new CGRect(x*10, y*10, 10, 10));

            }
        }


        UIImage img = UIGraphics.GetImageFromCurrentImageContext();

        UIGraphics.EndImageContext();

        return img;
    }
}