DataMatrix 条码格式在 ZXingBarcodeImageView Xamarin Forms Zxing 2.3.2 中模糊 [英] DataMatrix Barcode Format Blurred in ZXingBarcodeImageView Xamarin Forms Zxing 2.3.2

查看:27
本文介绍了DataMatrix 条码格式在 ZXingBarcodeImageView Xamarin Forms Zxing 2.3.2 中模糊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

截图

<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 本身上设置不同的高度和宽度.

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

有什么推荐吗?

推荐答案

这个 bug 在 ZXing 中仅适用于 DataMatrix,因此我最终只为 Android 和 iOS 创建了自定义渲染器(Windows 工作正常).

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).

安卓代码:

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

这篇关于DataMatrix 条码格式在 ZXingBarcodeImageView Xamarin Forms Zxing 2.3.2 中模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆