Pàgines

Apr 14, 2011

BarCode reading and writing with Processing



Mira aquest enllaç, però si el compiles tu, tindràs una verssió mes actualitzada.


// http://code.google.com/p/zxing/wiki/GettingStarted
// http://blog.makezine.com/archive/2011/03/codebox-use-qr-codes-in-processing.html
// http://code.google.com/p/zxing/wiki/DeveloperNotes
// http://zxing.org/w/docs/javadoc/index.html
// http://code.google.com/p/zxing/source/browse/trunk#trunk%2Fcore%2Fsrc
// http://zxing.org/w/docs/javadoc/com/google/zxing/client/j2se/package-frame.html
// http://code.google.com/p/zxing/source/browse/trunk#trunk%2Fjavase%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fclient%2Fj2se
// http://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java




import com.google.zxing.*;
import java.awt.image.BufferedImage;


int SIZE = 400;


//com.google.zxing.Writer writer = new com.google.zxing.qrcode.QRCodeWriter(); 
//http://zxing.org/w/docs/javadoc/index.html
//com.google.zxing.Reader reader = new com.google.zxing.qrcode.QRCodeReader();
MultiFormatWriter writer = new MultiFormatWriter();
MultiFormatReader reader = new MultiFormatReader();
BitMatrix QRBitMatrix = new BitMatrix(SIZE, SIZE);
BarcodeFormat format = BarcodeFormat.QR_CODE;
// QR_CODE
// DATA_MATRIX
// UPC_E
// UPC_A
// EAN_8
// EAN_13
// CODE_128
// CODE_39       ATENCIÓ    Només Numèric
// ITF






void setup(){
size(SIZE, SIZE);


PImage img = new PImage(SIZE, SIZE);
String codi = "Hola";//println(codi.length());


img = codificaBarCcode(codi, format, SIZE, SIZE);


image(img, 0, 0);
img.save(savePath("data\\QR.jpg"));


println(decodificaBarCode(img));
}






PImage codificaBarCcode(String codi, BarcodeFormat format, int ample, int alt){
color negre = color(0), blanc = color(255);
PImage img = new PImage(ample, alt);
Hashtable hints = new Hashtable(1);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

try {
QRBitMatrix = writer.encode(codi, format, ample, alt, hints);
} catch (Exception e) {println(e.toString());}

int width = QRBitMatrix.getWidth();
int height = QRBitMatrix.getHeight();

for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
if(QRBitMatrix.get(x, y)) img.set(x, y, negre); else img.set(x, y, blanc);
}
}
return(img);
}




String decodificaBarCode(PImage img){
String retorn = "";
Result result = null;

try {
LuminanceSource source = new BufferedImageLuminanceSource((BufferedImage)img.getImage());
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
result = reader.decode(bitmap);
retorn = result.getText();
if (retorn != null) {
//println(result.getText());
ResultPoint[] points = result.getResultPoints();

for (int i = 0; i < points.length; i++) {
fill(#ff8c00);
ellipse(points[i].getX(), points[i].getY(), 20,20);
}
}
} catch (Exception e) {println(e.toString());}

return(retorn);
}