Skip to content

Commit

Permalink
- added EMF support
Browse files Browse the repository at this point in the history
- increased reliablitity of the scanner
  • Loading branch information
gomi42 committed Aug 21, 2023
1 parent d4ee8a1 commit a1d1967
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 32 deletions.
147 changes: 147 additions & 0 deletions BarcodeGenerator/EmfBarcodeGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
//
// Author:
// Michael Göricke
//
// Copyright (c) 2023
//
// This file is part of FocalMaster.
//
// The FocalMaster is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see<http://www.gnu.org/licenses/>.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace FocalCompiler
{
internal class EmfBarcodeGenerator : BarcodeGenerator
{
private const float TopBorder = 0;
private const float LeftBorder = 0;
private const float ZeroBarWidth = 2;
private const float OneBarWidth = 2 * ZeroBarWidth;
private const float GapBarWidth = ZeroBarWidth;
private const float BarHeight = 33;
private const double RowsPerPage = 19;

/////////////////////////////////////////////////////////////

private string imageBaseFilename;
private Metafile metafile;
private Graphics graphics;
private int currentPage;
private int currentRowOnPage;
private float currentY;
private float currentX;

/////////////////////////////////////////////////////////////

public bool GenerateEmf(string focal, string outputBaseFilename)
{
imageBaseFilename = Path.Combine(Path.GetDirectoryName(outputBaseFilename), Path.GetFileNameWithoutExtension(outputBaseFilename));
currentPage = 1;

return Generate(focal, false);
}

/////////////////////////////////////////////////////////////

protected override void Save()
{
if (graphics == null)
{
return;
}

graphics.Dispose();
metafile.Dispose();

graphics = null;
metafile = null;
}

/////////////////////////////////////////////////////////////

private void InitImage()
{
Bitmap bitmap = new Bitmap(16, 16);
Graphics referenceGraphics = Graphics.FromImage(bitmap);
IntPtr referenceHdc = referenceGraphics.GetHdc();

metafile = new Metafile(imageBaseFilename + "-" + currentPage.ToString() + ".emf", referenceHdc, EmfType.EmfOnly);

referenceGraphics.ReleaseHdc(referenceHdc);
referenceGraphics.Dispose();
bitmap.Dispose();

graphics = Graphics.FromImage(metafile);

currentY = TopBorder;
currentRowOnPage = 0;
}

/////////////////////////////////////////////////////////////

protected override void BeginBarcodeRow(int currentRow, int fromLine, int toLine)
{
if (graphics == null)
{
InitImage();
}

string s = string.Format("Row {0} ({1} - {2})", currentRow, fromLine, toLine);

Font drawFont = new Font("Arial", 9);
SolidBrush drawBrush = new SolidBrush(Color.Black);

graphics.DrawString(s, drawFont, drawBrush, new PointF(LeftBorder, currentY + 3));

currentY += drawFont.Height + 2;
currentX = LeftBorder;
}

/////////////////////////////////////////////////////////////

protected override void EndBarcodeRow()
{
currentY += BarHeight;
currentRowOnPage++;

if (currentRowOnPage >= RowsPerPage)
{
Save();
currentPage++;
}
}

/////////////////////////////////////////////////////////////

protected override void AddZeroBar()
{
graphics.FillRectangle(Brushes.Black, currentX, currentY, ZeroBarWidth, BarHeight);

currentX += ZeroBarWidth + GapBarWidth;
}

/////////////////////////////////////////////////////////////

protected override void AddOneBar()
{
graphics.FillRectangle(Brushes.Black, currentX, currentY, OneBarWidth, BarHeight);

currentX += OneBarWidth + GapBarWidth;
}
}
}
2 changes: 0 additions & 2 deletions BarcodeGenerator/ImageBarcodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see<http://www.gnu.org/licenses/>.

using System;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using static System.Net.Mime.MediaTypeNames;

namespace FocalCompiler
{
Expand Down
Loading

0 comments on commit a1d1967

Please sign in to comment.