Como puedo generar un PDF417 codigo de barra
25 de febrero, 2008 - 13:48
tengo toda la semana pasada batallando con ese problema necesito generar un codigo de barra de 2 dimensiones y en verdad que no he podido, trabajando con java modifique una libreria para poder generar el codigo de barra pero ya cuando lo genera e intento leerlo no puedo simplemente no lo lee, ademas que me crea un codigo muy grande.
aqui pongo el codigo completo por si alguien me puede ayudar:
/*
* Copyright 2003-2005 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'pdf417lib, a library to generate the bidimensional barcode PDF417'.
*
* The Initial Developer of the Original Code is Paulo Soares. Portions created by
* the Initial Developer are Copyright (C) 2003 by Paulo Soares.
* All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library 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 Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://sourceforge.net/projects/pdf417lib
* This code is also used in iText (http://www.lowagie.com/iText)
*/
import java.util.ArrayList;
import java.io.UnsupportedEncodingException;
import java.awt.Color;
import java.awt.Canvas;
import java.awt.image.MemoryImageSource;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.FileReader;
import java.io.File;
import java.io.BufferedReader;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
/** Generates the 2D barcode PDF417. Supports dimensioning auto-sizing, fixed
* and variable sizes, automatic and manual error levels, raw codeword input,
* codeword size optimization and bitmap inversion. The output can
* be a CCITT G4 <CODE>Image</CODE> or a raw bitmap.
* author Paulo Soares (psoares consiste.pt)
*/
public class Pdf417lib {
/** Auto-size is made based on <CODE>aspectRatio</CODE> and <CODE>yHeight</CODE>. */
public static final int PDF417_USE_ASPECT_RATIO = 0;
/** The size of the barcode will be at least <CODE>codeColumns*codeRows</CODE>. */
public static final int PDF417_FIXED_RECTANGLE = 1;
/** The size will be at least <CODE>codeColumns</CODE>
* with a variable number of <CODE>codeRows</CODE>.
*/
public static final int PDF417_FIXED_COLUMNS = 2;
/** The size will be at least <CODE>codeRows</CODE>
* with a variable number of <CODE>codeColumns</CODE>.
*/
public static final int PDF417_FIXED_ROWS = 4;
/** The error level correction is set automatically according
* to ISO 15438 recomendations.
*/
public static final int PDF417_AUTO_ERROR_LEVEL = 0;
/** The error level correction is set by the user. It can be 0 to 8. */
public static final int PDF417_USE_ERROR_LEVEL = 16;
/** No <CODE>text</CODE> interpretation is done and the content of <CODE>codewords</CODE>
* is used directly.
*/
public static final int PDF417_USE_RAW_CODEWORDS = 64;
/** Inverts the output bits of the raw bitmap that is normally
* bit one for black. It has only effect for the raw bitmap.
*/
public static final int PDF417_INVERT_BITMAP = 128;
protected int bitPtr;
protected int cwPtr;
protected SegmentList segmentList;
/** Creates a new <CODE>BarcodePDF417</CODE> with the default settings. */
public Pdf417lib() {
setDefaultParameters();
}
protected boolean checkSegmentType(Segment segment, char type) {
if (segment == null)
return false;
return segment.type == type;
}
protected int getSegmentLength(Segment segment) {
if (segment == null)
return 0;
return segment.end - segment.start;
}
/** Set the default settings that correspond to <CODE>PDF417_USE_ASPECT_RATIO</CODE>
* and <CODE>PDF417_AUTO_ERROR_LEVEL</CODE>.
*/
public void setDefaultParameters() {
options = 0;
outBits = null;
text = new byte[0];
yHeight = 3;
aspectRatio = 0.5f;
}
protected void outCodeword17(int codeword) {
int bytePtr = bitPtr / 8;
int bit = bitPtr - bytePtr * 8;
outBits[bytePtr++] |= codeword >> (9 + bit);
outBits[bytePtr++] |= codeword >> (1 + bit);
codeword <<= 8;
outBits[bytePtr] |= codeword >> (1 + bit);
bitPtr += 17;
}
protected void outCodeword18(int codeword) {
int bytePtr = bitPtr / 8;
int bit = bitPtr - bytePtr * 8;
outBits[bytePtr++] |= codeword >> (10 + bit);
outBits[bytePtr++] |= codeword >> (2 + bit);
codeword <<= 8;
outBits[bytePtr] |= codeword >> (2 + bit);
if (bit == 7)
outBits[++bytePtr] |= 0x80;
bitPtr += 18;
}
protected void outCodeword(int codeword) {
outCodeword17(codeword);
}
protected void outStopPattern() {
outCodeword18(STOP_PATTERN);
}
protected void outStartPattern() {
outCodeword17(START_PATTERN);
}
protected void outPaintCode() {
int codePtr = 0;
bitColumns = START_CODE_SIZE * (codeColumns + 3) + STOP_SIZE;
int lenBits = ((bitColumns - 1) / 8 + 1) * codeRows;
outBits = new byte[lenBits];
for (int row = 0; row < codeRows; ++row) {
bitPtr = ((bitColumns - 1) / 8 + 1) * 8 * row;
int rowMod = row % 3;
int cluster[] = CLUSTERS[rowMod];
outStartPattern();
int edge = 0;
switch (rowMod) {
case 0:
edge = 30 * (row / 3) + ((codeRows - 1) / 3);
break;
case 1:
edge = 30 * (row / 3) + errorLevel * 3 + ((codeRows - 1) % 3);
break;
default:
edge = 30 * (row / 3) + codeColumns - 1;
break;
}
outCodeword(cluster[edge]);
for (int column = 0; column < codeColumns; ++column) {
outCodeword(cluster[codewords[codePtr++]]);
}
switch (rowMod) {
case 0:
edge = 30 * (row / 3) + codeColumns - 1;
break;
case 1:
edge = 30 * (row / 3) + ((codeRows - 1) / 3);
break;
default:
edge = 30 * (row / 3) + errorLevel * 3 + ((codeRows - 1) % 3);
break;
}
outCodeword(cluster[edge]);
outStopPattern();
}
if ((options & PDF417_INVERT_BITMAP) != 0) {
for (int k = 0; k < outBits.length; ++k)
outBits[k] ^= 0xff;
}
}
protected void calculateErrorCorrection(int dest) {
if (errorLevel < 0 || errorLevel > 8)
errorLevel = 0;
int A[] = ERROR_LEVEL[errorLevel];
int Alength = 2 << errorLevel;
for (int k = 0; k < Alength; ++k)
codewords[dest + k] = 0;
int lastE = Alength - 1;
for (int k = 0; k < lenCodewords; ++k) {
int t1 = codewords[k] + codewords[dest];
for (int e = 0; e <= lastE; ++e) {
int t2 = (t1 * A[lastE - e]) % MOD;
int t3 = MOD - t2;
codewords[dest + e] = ((e == lastE ? 0 : codewords[dest + e + 1]) + t3) % MOD;
}
}
for (int k = 0; k < Alength; ++k)
codewords[dest + k] = (MOD - codewords[dest + k]) % MOD;
}
protected int getTextTypeAndValue(int maxLength, int idx) {
if (idx >= maxLength)
return 0;
char c = (char)(text[idx] & 0xff);
if (c >= 'A' && c <= 'Z')
return (ALPHA + c - 'A');
if (c >= 'a' && c <= 'z')
return (LOWER + c - 'a');
if (c == ' ')
return (ALPHA + LOWER + MIXED + SPACE);
int ms = MIXED_SET.indexOf(c);
int ps = PUNCTUATION_SET.indexOf(c);
if (ms < 0 && ps < 0)
return (ISBYTE + c);
if (ms == ps)
return (MIXED + PUNCTUATION + ms);
if (ms >= 0)
return (MIXED + ms);
return (PUNCTUATION + ps);
}
protected void textCompaction(int start, int length) {
int dest[] = new int[ABSOLUTE_MAX_TEXT_SIZE * 2];
int mode = ALPHA;
int ptr = 0;
int fullBytes = 0;
int v = 0;
int k;
int size;
length += start;
for (k = start; k < length; ++k) {
v = getTextTypeAndValue(length, k);
if ((v & mode) != 0) {
dest[ptr++] = v & 0xff;
continue;
}
if ((v & ISBYTE) != 0) {
if ((ptr & 1) != 0) {
dest[ptr++] = (mode & PUNCTUATION) != 0 ? PAL : PS;
mode = (mode & PUNCTUATION) != 0 ? ALPHA : mode;
}
dest[ptr++] = BYTESHIFT;
dest[ptr++] = v & 0xff;
fullBytes += 2;
continue;
}
switch (mode) {
case ALPHA:
if ((v & LOWER) != 0) {
dest[ptr++] = LL;
dest[ptr++] = v & 0xff;
mode = LOWER;
}
else if ((v & MIXED) != 0) {
dest[ptr++] = ML;
dest[ptr++] = v & 0xff;
mode = MIXED;
}
else if ((getTextTypeAndValue(length, k + 1) & getTextTypeAndValue(length, k + 2) & PUNCTUATION) != 0) {
dest[ptr++] = ML;
dest[ptr++] = PL;
dest[ptr++] = v & 0xff;
mode = PUNCTUATION;
}
else {
dest[ptr++] = PS;
dest[ptr++] = v & 0xff;
}
break;
case LOWER:
if ((v & ALPHA) != 0) {
if ((getTextTypeAndValue(length, k + 1) & getTextTypeAndValue(length, k + 2) & ALPHA) != 0) {
dest[ptr++] = ML;
dest[ptr++] = AL;
mode = ALPHA;
}
else {
dest[ptr++] = AS;
}
dest[ptr++] = v & 0xff;
}
else if ((v & MIXED) != 0) {
dest[ptr++] = ML;
dest[ptr++] = v & 0xff;
mode = MIXED;
}
else if ((getTextTypeAndValue(length, k + 1) & getTextTypeAndValue(length, k + 2) & PUNCTUATION) != 0) {
dest[ptr++] = ML;
dest[ptr++] = PL;
dest[ptr++] = v & 0xff;
mode = PUNCTUATION;
}
else {
dest[ptr++] = PS;
dest[ptr++] = v & 0xff;
}
break;
case MIXED:
if ((v & LOWER) != 0) {
dest[ptr++] = LL;
dest[ptr++] = v & 0xff;
mode = LOWER;
}
else if ((v & ALPHA) != 0) {
dest[ptr++] = AL;
dest[ptr++] = v & 0xff;
mode = ALPHA;
}
else if ((getTextTypeAndValue(length, k + 1) & getTextTypeAndValue(length, k + 2) & PUNCTUATION) != 0) {
dest[ptr++] = PL;
dest[ptr++] = v & 0xff;
mode = PUNCTUATION;
}
else {
dest[ptr++] = PS;
dest[ptr++] = v & 0xff;
}
break;
case PUNCTUATION:
dest[ptr++] = PAL;
mode = ALPHA;
--k;
break;
}
}
if ((ptr & 1) != 0)
dest[ptr++] = PS;
size = (ptr + fullBytes) / 2;
if (size + cwPtr > MAX_DATA_CODEWORDS) {
throw new IndexOutOfBoundsException("The text is too big.");
}
length = ptr;
ptr = 0;
while (ptr < length) {
v = dest[ptr++];
if (v >= 30) {
codewords[cwPtr++] = v;
codewords[cwPtr++] = dest[ptr++];
}
else
codewords[cwPtr++] = v * 30 + dest[ptr++];
}
}
protected void basicNumberCompaction(int start, int length) {
int ret = cwPtr;
int retLast = length / 3;
int ni, k;
cwPtr += retLast + 1;
for (k = 0; k <= retLast; ++k)
codewords[ret + k] = 0;
codewords[ret + retLast] = 1;
length += start;
for (ni = start; ni < length; ++ni) {
// multiply by 10
for (k = retLast; k >= 0; --k)
codewords[ret + k] *= 10;
// add the digit
codewords[ret + retLast] += text[ni] - '0';
// propagate carry
for (k = retLast; k > 0; --k) {
codewords[ret + k - 1] += codewords[ret + k] / 900;
codewords[ret + k] %= 900;
}
}
}
protected void numberCompaction(int start, int length) {
int full = (length / 44) * 15;
int size = length % 44;
int k;
if (size == 0)
size = full;
else
size = full + size / 3 + 1;
if (size + cwPtr > MAX_DATA_CODEWORDS) {
throw new IndexOutOfBoundsException("The text is too big.");
}
length += start;
for (k = start; k < length; k += 44) {
size = length - k < 44 ? length - k : 44;
basicNumberCompaction(k, size);
}
}
protected void byteCompaction6(int start) {
int length = 6;
int ret = cwPtr;
int retLast = 4;
int ni, k;
cwPtr += retLast + 1;
for (k = 0; k <= retLast ; ++k)
codewords[ret + k] = 0;
length += start;
for (ni = start; ni < length; ++ni) {
// multiply by 256
for (k = retLast; k >= 0; --k)
codewords[ret + k] *= 256;
// add the digit
codewords[ret + retLast] += (int)text[ni] & 0xff;
// propagate carry
for (k = retLast; k > 0; --k) {
codewords[ret + k - 1] += codewords[ret + k] / 900;
codewords[ret + k] %= 900;
}
}
}
void byteCompaction(int start, int length) {
int k, j;
int size = (length / 6) * 5 + (length % 6);
if (size + cwPtr > MAX_DATA_CODEWORDS) {
throw new IndexOutOfBoundsException("The text is too big.");
}
length += start;
for (k = start; k < length; k += 6) {
size = length - k < 44 ? length - k : 6;
if (size < 6) {
for (j = 0; j < size; ++j)
codewords[cwPtr++] = (int)text[k + j] & 0xff;
}
else {
byteCompaction6(k);
}
}
}
void breakString() {
int textLength = text.length;
int lastP = 0;
int startN = 0;
int nd = 0;
char c = 0;
int k, ptrS, j;
boolean lastTxt, txt;
Segment v;
Segment vp;
Segment vn;
for (k = 0; k < textLength; ++k) {
c = (char)(text[k] & 0xff);
if (c >= '0' && c <= '9') {
if (nd == 0)
startN = k;
++nd;
continue;
}
if (nd >= 13) {
if (lastP != startN) {
c = (char)(text[lastP] & 0xff);
ptrS = lastP;
lastTxt = (c >= ' ' && c < 127) || c == 'r' || c == 'n' || c == 't';
for (j = lastP; j < startN; ++j) {
c = (char)(text[j] & 0xff);
txt = (c >= ' ' && c < 127) || c == 'r' || c == 'n' || c == 't';
if (txt != lastTxt) {
segmentList.add(lastTxt ? 'T' : 'B', lastP, j);
lastP = j;
lastTxt = txt;
}
}
segmentList.add(lastTxt ? 'T' : 'B', lastP, startN);
}
segmentList.add('N', startN, k);
lastP = k;
}
nd = 0;
}
if (nd < 13)
startN = textLength;
if (lastP != startN) {
c = (char)(text[lastP] & 0xff);
ptrS = lastP;
lastTxt = (c >= ' ' && c < 127) || c == 'r' || c == 'n' || c == 't';
for (j = lastP; j < startN; ++j) {
c = (char)(text[j] & 0xff);
txt = (c >= ' ' && c < 127) || c == 'r' || c == 'n' || c == 't';
if (txt != lastTxt) {
segmentList.add(lastTxt ? 'T' : 'B', lastP, j);
lastP = j;
lastTxt = txt;
}
}
segmentList.add(lastTxt ? 'T' : 'B', lastP, startN);
}
if (nd >= 13)
segmentList.add('N', startN, textLength);
//optimize
//merge short binary
for (k = 0; k < segmentList.size(); ++k) {
v = segmentList.get(k);
vp = segmentList.get(k - 1);
vn = segmentList.get(k + 1);;
if (checkSegmentType(v, 'B') && getSegmentLength(v) == 1) {
if (checkSegmentType(vp, 'T') && checkSegmentType(vn, 'T')
&& getSegmentLength(vp) + getSegmentLength(vn) >= 3) {
vp.end = vn.end;
segmentList.remove(k);
segmentList.remove(k);
k = -1;
continue;
}
}
}
//merge text sections
for (k = 0; k < segmentList.size(); ++k) {
v = segmentList.get(k);
vp = segmentList.get(k - 1);
vn = segmentList.get(k + 1);;
if (checkSegmentType(v, 'T') && getSegmentLength(v) >= 5) {
boolean redo = false;
if ((checkSegmentType(vp, 'B') && getSegmentLength(vp) == 1) || checkSegmentType(vp, 'T')) {
redo = true;
v.start = vp.start;
segmentList.remove(k - 1);
--k;
}
if ((checkSegmentType(vn, 'B') && getSegmentLength(vn) == 1) || checkSegmentType(vn, 'T')) {
redo = true;
v.end = vn.end;
segmentList.remove(k + 1);
}
if (redo) {
k = -1;
continue;
}
}
}
//merge binary sections
for (k = 0; k < segmentList.size(); ++k) {
v = segmentList.get(k);
vp = segmentList.get(k - 1);
vn = segmentList.get(k + 1);;
if (checkSegmentType(v, 'B')) {
boolean redo = false;
if ((checkSegmentType(vp, 'T') && getSegmentLength(vp) < 5) || checkSegmentType(vp, 'B')) {
redo = true;
v.start = vp.start;
segmentList.remove(k - 1);
--k;
}
if ((checkSegmentType(vn, 'T') && getSegmentLength(vn) < 5) || checkSegmentType(vn, 'B')) {
redo = true;
v.end = vn.end;
segmentList.remove(k + 1);
}
if (redo) {
k = -1;
continue;
}
}
}
// check if all numbers
if (segmentList.size() == 1 && (v = segmentList.get(0)).type == 'T' && getSegmentLength(v) >= 8) {
for (k = v.start; k < v.end; ++k) {
c = (char)(text[k] & 0xff);
if (c < '0' || c > '9')
break;
}
if (k == v.end)
v.type = 'N';
}
}
protected void assemble() {
int k;
if (segmentList.size() == 0)
return;
cwPtr = 1;
for (k = 0; k < segmentList.size(); ++k) {
Segment v = segmentList.get(k);
switch (v.type) {
case 'T':
if (k != 0)
codewords[cwPtr++] = TEXT_MODE;
textCompaction(v.start, getSegmentLength(v));
break;
case 'N':
codewords[cwPtr++] = NUMERIC_MODE;
numberCompaction(v.start, getSegmentLength(v));
break;
case 'B':
codewords[cwPtr++] = (getSegmentLength(v) % 6) != 0 ? BYTE_MODE : BYTE_MODE_6;
byteCompaction(v.start, getSegmentLength(v));
break;
}
}
}
protected static int maxPossibleErrorLevel(int remain) {
int level = 8;
int size = 512;
while (level > 0) {
if (remain >= size)
return level;
--level;
size >>= 1;
}
return 0;
}
protected void dumpList() {
if (segmentList.size() == 0)
return;
for (int k = 0; k < segmentList.size(); ++k) {
Segment v = segmentList.get(k);
int len = getSegmentLength(v);
char c[] = new char[len];
for (int j = 0; j < len; ++j) {
c[j] = (char)(text[v.start + j] & 0xff);
if (c[j] == 'r')
c[j] = 'n';
}
System.out.println("" + v.type + new String(c));
}
}
protected int getMaxSquare() {
if (codeColumns > 21) {
codeColumns = 29;
codeRows = 32;
}
else {
codeColumns = 16;
codeRows = 58;
}
return MAX_DATA_CODEWORDS + 2;
}
/** Paints the barcode. If no exception was thrown a valid barcode is available. */
public void paintCode() {
int maxErr, lenErr, tot, pad;
if ((options & PDF417_USE_RAW_CODEWORDS) != 0) {
if (lenCodewords > MAX_DATA_CODEWORDS || lenCodewords < 1 || lenCodewords != codewords[0]) {
throw new IllegalArgumentException("Invalid codeword size.");
}
}
else {
if (text == null)
throw new NullPointerException("Text cannot be null.");
if (text.length > ABSOLUTE_MAX_TEXT_SIZE) {
throw new IndexOutOfBoundsException("The text is too big.");
}
segmentList = new SegmentList();
breakString();
//dumpList();
assemble();
segmentList = null;
codewords[0] = lenCodewords = cwPtr;
}
maxErr = maxPossibleErrorLevel(MAX_DATA_CODEWORDS + 2 - lenCodewords);
if ((options & PDF417_USE_ERROR_LEVEL) == 0) {
if (lenCodewords < 41)
errorLevel = 2;
else if (lenCodewords < 161)
errorLevel = 3;
else if (lenCodewords < 321)
errorLevel = 4;
else
errorLevel = 5;
}
if (errorLevel < 0)
errorLevel = 0;
else if (errorLevel > maxErr)
errorLevel = maxErr;
if (codeColumns < 1)
codeColumns = 1;
else if (codeColumns > 30)
codeColumns = 30;
if (codeRows < 3)
codeRows = 3;
else if (codeRows > 90)
codeRows = 90;
lenErr = 2 << errorLevel;
boolean fixedColumn = (options & PDF417_FIXED_ROWS) == 0;
boolean skipRowColAdjust = false;
tot = lenCodewords + lenErr;
if ((options & PDF417_FIXED_RECTANGLE) != 0) {
tot = codeColumns * codeRows;
if (tot > MAX_DATA_CODEWORDS + 2) {
tot = getMaxSquare();
}
if (tot < lenCodewords + lenErr)
tot = lenCodewords + lenErr;
else
skipRowColAdjust = true;
}
else if ((options & (PDF417_FIXED_COLUMNS | PDF417_FIXED_ROWS)) == 0) {
double c, b;
fixedColumn = true;
if (aspectRatio < 0.001)
aspectRatio = 0.001f;
else if (aspectRatio > 1000)
 
General
Comparte esta entrada (del.icio.us, por correo, etc) o agrega este blog a tu Google Reader.
Entradas relacionadas:
Neiman-Marcus Cookies House Recipe
bueno cai en la trampa
Problemas con PHP
Pollos geeks
Casi casi me contratan!!!!!
que es esto???
Chiste
Mi pasaporte!
Como controlar a una nina hiperactiva
Como extrano hacer esto
Poema de Erika
saludos desde Madison, Winsconsin
Problemas con Outlook 2002 (XP)
Pregunta
Brujita
Han escrito 3 comentarios de «Como puedo generar un PDF417 codigo de barra»