DataMatrix Barcode Format Blurred in ZXingBarcodeImageView Xamarin Forms Zxing 2.3.2

泪湿孤枕 提交于 2019-12-24 18:31:27

问题


I'm trying to display DataMatrix barcode inside ZXingBarcodeImageView, the problem is whatever width and height I use the barcode is blurry with low resolution.

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>

I tried to set different Height and Width in EncodingOptions and on the ZXingBarcodeImageView itself.

Any recommendation?


回答1:


I had a similar issue.

Turns out you can set the EncodingOptions like this for example:

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

Because generation happens at a lower level and it gets scaled up.

This answer helped me: how to fix an unclear qr code image generated using zxing 2.1?




回答2:


This bug is in ZXing portable only for DataMatrix so I ended up by creating custom renderers for Android and iOS only(Windows works fine).

Android Code:

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

On 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;
    }
}


来源:https://stackoverflow.com/questions/48505790/datamatrix-barcode-format-blurred-in-zxingbarcodeimageview-xamarin-forms-zxing-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!