Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

font not read #45

Open
teunlielu opened this issue May 16, 2018 · 15 comments
Open

font not read #45

teunlielu opened this issue May 16, 2018 · 15 comments
Assignees
Labels

Comments

@teunlielu
Copy link
Contributor

I have two bugs fond:

  1. you must have: b.AlternateLabel = ""
    null give an error about null exeption

(RawData.StartsWith(AlternateLabel) || (AlternateLabel == null)
startWith go first after the null check
I cann't by past beter

  1. you name size of 100 by 100 ean 13 and genereate label.
    you cann't read the first nummer of barcode label
    problem is by more sizes.
    example on this sreentshot
    image

Thanks

@teunlielu
Copy link
Contributor Author

teunlielu commented May 18, 2018

Is possible solution to problem to solve it:
first number before the barcode lines:
What you can see here:

image

@LordPinhead
Copy link

Possible solution could be printing the Numbers above the Barcode Layer and add some White Space at starting and ending of the Numbercode. Or the solution you showed but I never saw that on an barcode or in an Barcode lib in my life.

I had in my previous work a fork solving some problems with the resolution, maybe setting the Image Resolution to 300 px could solve it. Unfortunately i quit before getting my changes out.

@barnhill
Copy link
Owner

The problem with just adding arbitrary space before and after the barcode is that the consumer has specified a specific width for the barcode image to be and adding space for the number is either going to:

  1. increase this beyond what is specified to accommodate this space.
  2. encroach on the space to draw the barcode and shrink the barcode width itself more.

Im not sure which or if either is really acceptable. Just trying to weigh options here.

@teunlielu
Copy link
Contributor Author

Sorry for late response. best is to take up space including for numbers such as size. so space becomes smaller.
These belong to the barcode. Behelva if one chooses not to mention text.
@LordPinhead have you a fixe?

@rob313663
Copy link
Contributor

rob313663 commented Oct 28, 2019

image

@teunlielu, what you used to generate that barcode is also not correct. There should be a spacing after the 8.

@rob313663
Copy link
Contributor

The above code was generated with a font where the bars and spaces are defined as separate glyphs, together with the OCR font digit below it. @barnhill has made a library for generating pixel based images, not vector based, so any text (vector based) used as human readable text below the bars and spaces will probably never be perfect.

image

@teunlielu
Copy link
Contributor Author

@rob313663 how would you solve? Vectorbase is not option. I think so. Is better with space but to smalle for hunan for reading.

@rob313663
Copy link
Contributor

rob313663 commented Oct 28, 2019

@teunlielu Personally I already solved it. I made a font, exactly following the specifications of the EAN/UPC codes. I did that in 1997. Those fonts are not open source so I can't give it to you, we even stopped selling it because it generated too much support (never give a sharp knife to a child, I got a sharp knife, still have a scar to remind me).

barcodelib is for generating images of barcodes, the machine readable part, which I think it does in a very good way.

Making the human readable part in barcodes, especially UPC/EAN is very hard unless you go for a font, which barcodelib is not.

@rob313663
Copy link
Contributor

@teunlielu And I am not a developer of barcodelib, or even a contributor, just trying to pitch in with my knowledge of barcodes.

@rob313663
Copy link
Contributor

rob313663 commented Oct 29, 2019

@teunlielu will something like this do?

            var b = new BarcodeLib.Barcode();
            var i = b.Encode(BarcodeLib.TYPE.EAN13, "8901072002478");

            Image resultImage = new Bitmap(i.Width+200, i.Height+50, PixelFormat.Format24bppRgb);

            Font font = new Font("Courier New", 26 * 1.33f, FontStyle.Regular, GraphicsUnit.Pixel);
            SolidBrush drawBrush = new SolidBrush(Color.Black);
            float x = 100f - 35f;
            float y = i.Height;

            string humanReadable = "8 901072 002478";
            using (Graphics g = Graphics.FromImage(resultImage))
            {
                g.FillRectangle( Brushes.White, 0, 0, resultImage.Width, resultImage.Height);
                g.DrawImage(i, 100, 0);
                g.DrawString(humanReadable, font, drawBrush, x, y);
            }

            resultImage.Save(@"C:\temp\ean13.png", ImageFormat.Png);

image

@teunlielu
Copy link
Contributor Author

I have works at the moment old versions this lib. Give this same output of your. New idea of barcode format is beautyfuller. But old works for me fine. We have two options change code render old version or is possible to fix this bug.

@bmojanoski
Copy link

Is there a solution for second bug?

@barnhill
Copy link
Owner

The label logic needs some work Im afraid and all barcode types have different label formats

@teunlielu
Copy link
Contributor Author

My suggestion is:

change 0,5f to 0,72f in follow code
then you end up with dashes above just 100%, but I think this is fairly easy to adjust
@barnhill ?

    /// <summary>
    /// Draws Label for EAN-13 barcodes
    /// </summary>
    /// <param name="img">Image representation of the barcode without the labels</param>
    /// <returns>Image representation of the barcode with labels applied</returns>
    public static Image Label_EAN13(Barcode Barcode, Bitmap img)
    {
        try
        {
            int iBarWidth = Barcode.Width / Barcode.EncodedValue.Length;
            string defTxt = Barcode.RawData;

            using (Font labFont = new Font("Arial", getFontsize(Barcode, Barcode.Width - Barcode.Width % Barcode.EncodedValue.Length, img.Height, defTxt) * Barcode.DotsPerPointAt96Dpi, FontStyle.Regular, GraphicsUnit.Pixel))
            {
                int shiftAdjustment;
                switch (Barcode.Alignment)
                {
                    case AlignmentPositions.LEFT:
                        shiftAdjustment = 0;
                        break;
                    case AlignmentPositions.RIGHT:
                        shiftAdjustment = (Barcode.Width % Barcode.EncodedValue.Length);
                        break;
                    case AlignmentPositions.CENTER:
                    default:
                        shiftAdjustment = (Barcode.Width % Barcode.EncodedValue.Length) / 2;
                        break;
                }//switch

                using (Graphics g = Graphics.FromImage(img))
                {
                    g.DrawImage(img, (float)0, (float)0);

                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

                    StringFormat f = new StringFormat
                    {
                        Alignment = StringAlignment.Near,
                        LineAlignment = StringAlignment.Near
                    };
                    int LabelY = 0;

                    //Default alignment for EAN13
                    LabelY = img.Height - labFont.Height;
                    f.Alignment = StringAlignment.Near;

                    float w1 = iBarWidth * 4; //Width of first block
                    float w2 = iBarWidth * 42; //Width of second block
                    float w3 = iBarWidth * 42; //Width of third block

                    float s1 = shiftAdjustment - iBarWidth;
                    float s2 = s1 + (iBarWidth * 4); //Start position of block 2
                    float s3 = s2 + w2 + (iBarWidth * 5); //Start position of block 3

                    //Draw the background rectangles for each block
                    using (SolidBrush backBrush = new SolidBrush(Barcode.BackColor))
                    {
                        g.FillRectangle(backBrush, new RectangleF(s2, (float)LabelY, w2, (float)labFont.Height));
                        g.FillRectangle(backBrush, new RectangleF(s3, (float)LabelY, w3, (float)labFont.Height));

                    }

                    //draw datastring under the barcode image
                    using (SolidBrush foreBrush = new SolidBrush(Barcode.ForeColor))
                    {


                        using (Font smallFont = new Font(labFont.FontFamily, labFont.SizeInPoints * 0.72f * Barcode.DotsPerPointAt96Dpi, labFont.Style, GraphicsUnit.Pixel))
                        {




                            g.DrawString(defTxt.Substring(0, 1), smallFont, foreBrush, new RectangleF(s1, (float)img.Height - (float)(smallFont.Height * 0.9), (float)img.Width, (float)labFont.Height), f);
                        }
                        g.DrawString(defTxt.Substring(1, 6), labFont, foreBrush, new RectangleF(s2, (float)LabelY, (float)img.Width, (float)labFont.Height), f);
                        g.DrawString(defTxt.Substring(7), labFont, foreBrush, new RectangleF(s3 - iBarWidth, (float)LabelY, (float)img.Width, (float)labFont.Height), f);
                    }

                    g.Save();
                }
            }//using
            return img;
        }//try
        catch (Exception ex)
        {
            throw new Exception("ELABEL_EAN13-1: " + ex.Message);
        }//catch
    }//Label_EAN13`

image

image

@barnhill
Copy link
Owner

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants