📄 barcoder.c
字号:
/*
pdf417codec library to encode/decode pdf417 barcodes
Copyright (C) 2005 Michael Butscher (mbutscher@gmx.de)
(see additional copyright notices in Copyright.txt)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include "FreeImage.h"
#include "pdf417codec.h"
/** Convert 8 bit image data from FreeImage format to
pdf417 format.
@param dib an 8bit gray image from FreeImage library
@return handle to newly created pdf417 gray image
*/
uint fiToPdf417(FIBITMAP *dib)
{
ubyte* src = FreeImage_GetBits(dib);
uint pitch = FreeImage_GetPitch(dib);
uint width = FreeImage_GetWidth(dib);
uint height = FreeImage_GetHeight(dib);
uint y;
ubyte* dst = malloc(width * height);
for (y = 0; y < height; y++)
{
memcpy(dst + (y * width),
src + ((height - y - 1) * pitch),
width);
}
uint img_hdl = pdf417_createImage(width, height, dst);
free(dst);
dst = NULL;
return img_hdl;
}
/** Convert 8 bit image data from pdf417 format to
FreeImage format.
@param img_hdl handle to pdf417 gray image
@return pointer to newly created FreeImage 8bit image.
*/
FIBITMAP *pdf417ToFi(uint img_hdl)
{
int width = pdf417_getImageWidth(img_hdl);
int height = pdf417_getImageHeight(img_hdl);
FIBITMAP *result = FreeImage_Allocate(width, height, 8, 0, 0, 0);
uint pitch = FreeImage_GetPitch(result);
ubyte *dst = FreeImage_GetBits(result);
ubyte *src = malloc(width * height);
uint y;
int i;
pdf417_getImageDataCopy(img_hdl, src);
for (y = 0; y < height; y++)
{
memcpy(dst + (y * pitch),
src + ((height - y - 1) * width),
width);
}
free(src);
src = NULL;
RGBQUAD *pal = FreeImage_GetPalette(result);
for (i = 0; i < 256; i++) {
pal[i].rgbRed = i;
pal[i].rgbGreen = i;
pal[i].rgbBlue = i;
}
return result;
}
void printUsage(void)
{
printf("\
barcoder <mode> <image name> [<text for encoder>]\n\
\n\
mode -- either 'e' for encode or 'd' for decode\n\
image name -- name of image to produce/read\n\
text -- text to encode in the barcode\n");
}
int main(int argc, char *argv[])
{
if (argc < 3)
{
printUsage();
return 0;
}
if (argv[1][0] == 'e') // Encode
{
if (argc < 4)
{
printUsage();
return 0;
}
uint img_hdl = pdf417_encodePlain(-1, 5, 5, 0, argv[3], -1);
uint img_hdl2 = pdf417_imageScale(img_hdl, 8, 8);
const int border = 30;
uint img_hdl3 = pdf417_imageBorder(img_hdl2, border, border, border, border, 255);
pdf417_freeHandle(img_hdl);
pdf417_freeHandle(img_hdl2);
FIBITMAP *dib = pdf417ToFi(img_hdl3);
pdf417_freeHandle(img_hdl3);
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
fif = FreeImage_GetFIFFromFilename(argv[2]);
FreeImage_Save(fif, dib, argv[2], 0);
FreeImage_Unload(dib);
}
else if (argv[1][0] == 'd') // Decode
{
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
// check the file signature and deduce its format
// (the second argument is currently not used by FreeImage)
fif = FreeImage_GetFileType(argv[2], 0);
if(fif == FIF_UNKNOWN)
{
// no signature ?
// try to guess the file format from the file extension
fif = FreeImage_GetFIFFromFilename(argv[2]);
}
FIBITMAP *dibr = FreeImage_Load(fif, argv[2], 0);
FIBITMAP *dib = FreeImage_ConvertTo8Bits(dibr);
FreeImage_Unload(dibr);
uint img_hdl = fiToPdf417(dib);
FreeImage_Unload(dib);
uint txt_hdl = pdf417_decodePlain(img_hdl);
pdf417_freeHandle(img_hdl);
char* text = malloc(pdf417_getStrSize(txt_hdl) + 1);
pdf417_getStrDataCopy(txt_hdl, text);
text[pdf417_getStrSize(txt_hdl)] = '\0';
pdf417_freeHandle(txt_hdl);
printf("Decoded: %s\n", text);
free(text);
text = NULL;
}
else
printUsage();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -