在ZXingBarcodeImageView中模糊的DataMatrix条形码格式Xamarin表示Zxing 2.3.2

时间:2018-01-29 16:27:02

标签: xamarin.forms zxing.net

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

Screenshot

public Object filter(MethodHandle handle, Object ... args)
{
    Object ret = handle.invoke(args);
    System.out.printf("# filter((%s) %s)%n", ret.getClass().getName(), ret);
    return ret;
}

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

任何建议?

2 个答案:

答案 0 :(得分:1)

我有类似的问题。

原则上你可以设置像这样的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?

答案 1 :(得分:0)

此错误仅在ZXing便携式数据库中可用于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;
    }
}
相关问题