siccome mi sono un po' intrippato con sta libreria, ed avendo riscontrato che la versione online riusciva a decodificare alcuni QR che invece il semplice codice linkato non decifrava, ho spulciato un po' nella distribuzione ed ho trovato il codice della servlet che usano nella pagina ufficiale del progetto. Strippando di tutto quanto non "algoritmico", ma non ripulendo (per noia) interamente il codice, viene fuori questo - che funziona meglio di quello linkato:
codice:
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.*;
import com.google.zxing.multi.*;
import java.util.*;
public class QRReader {
private static final Map<DecodeHintType,Object> HINTS;
private static final Map<DecodeHintType,Object> HINTS_PURE;
static {
HINTS = new EnumMap<DecodeHintType,Object>(DecodeHintType.class);
HINTS.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
HINTS.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
HINTS_PURE = new EnumMap<DecodeHintType,Object>(HINTS);
HINTS_PURE.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
}
public static void main(String args[]) {
String filePath = "C:/Users/Andrea/Desktop/QRCode.jpg";
BufferedImage image = null;
try {
image = ImageIO.read(new File(filePath));
}
catch (Exception e) {
e.printStackTrace();
}
Reader reader = new MultiFormatReader();
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
Collection<Result> results = new ArrayList<Result>(1);
ReaderException savedException = null;
try {
// Look for multiple barcodes
MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);
if (theResults != null) {
results.addAll(Arrays.asList(theResults));
}
}
catch (ReaderException re) {
savedException = re;
}
if (results.isEmpty()) {
try {
// Look for pure barcode
Result theResult = reader.decode(bitmap, HINTS_PURE);
if (theResult != null) {
results.add(theResult);
}
}
catch (ReaderException re) {
savedException = re;
}
}
if (results.isEmpty()) {
try {
// Look for normal barcode in photo
Result theResult = reader.decode(bitmap, HINTS);
if (theResult != null) {
results.add(theResult);
}
}
catch (ReaderException re) {
savedException = re;
}
}
if (results.isEmpty()) {
try {
// Try again with other binarizer
BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source));
Result theResult = reader.decode(hybridBitmap, HINTS);
if (theResult != null) {
results.add(theResult);
}
}
catch (ReaderException re) {
savedException = re;
}
}
if (savedException != null) {
System.out.println(savedException);
}
System.out.println("Decodificato: ");
for (Result r : results) {
System.out.println(r);
}
}
}