use of jp.sourceforge.qrcode.exception.DecodingFailedException in project qrcode by yanbe.
the class QRCodeDecoder method decode.
DecodeResult decode(QRCodeImage qrCodeImage, Point adjust) throws DecodingFailedException {
try {
if (numTryDecode == 0) {
canvas.println("Decoding started");
int[][] intImage = imageToIntArray(qrCodeImage);
imageReader = new QRCodeImageReader();
qrCodeSymbol = imageReader.getQRCodeSymbol(intImage);
} else {
canvas.println("--");
canvas.println("Decoding restarted #" + (numTryDecode));
qrCodeSymbol = imageReader.getQRCodeSymbolWithAdjustedGrid(adjust);
}
} catch (SymbolNotFoundException e) {
throw new DecodingFailedException(e.getMessage());
}
canvas.println("Created QRCode symbol.");
canvas.println("Reading symbol.");
canvas.println("Version: " + qrCodeSymbol.getVersionReference());
canvas.println("Mask pattern: " + qrCodeSymbol.getMaskPatternRefererAsString());
// blocks contains all (data and RS) blocks in QR Code symbol
int[] blocks = qrCodeSymbol.getBlocks();
canvas.println("Correcting data errors.");
// now blocks turn to data blocks (corrected and extracted from original blocks)
blocks = correctDataBlocks(blocks);
try {
byte[] decodedByteArray = getDecodedByteArray(blocks, qrCodeSymbol.getVersion(), qrCodeSymbol.getNumErrorCollectionCode());
return new DecodeResult(decodedByteArray, numLastCorrectionFailures);
} catch (InvalidDataBlockException e) {
canvas.println(e.getMessage());
throw new DecodingFailedException(e.getMessage());
}
}
use of jp.sourceforge.qrcode.exception.DecodingFailedException in project qrcode by yanbe.
the class J2SEDebugCanvas method actionPerformed.
public void actionPerformed(ActionEvent event) {
if (event.getSource().equals(openMenu)) {
chooser.showOpenDialog(this);
if (chooser.getSelectedFile() == null)
return;
try {
sourceImage = ImageIO.read(chooser.getSelectedFile());
} catch (Exception e) {
e.printStackTrace();
}
} else if (event.getActionCommand().equals("Open from URL")) {
try {
sourceImage = ImageIO.read(new URL(url.getText()));
} catch (Exception e) {
e.printStackTrace();
}
} else
return;
if (sourceImageLabel != null)
getContentPane().remove(sourceImageLabel);
sourceImageLabel = new JLabel(new ImageIcon(sourceImage));
getContentPane().add(sourceImageLabel, BorderLayout.WEST);
QRCodeDecoder decoder = new QRCodeDecoder();
if (canvas != null) {
getContentPane().remove(canvas);
//canvas.setImage(null);
}
canvas = new J2SEDebugCanvas();
QRCodeDecoder.setCanvas(canvas);
getContentPane().add(canvas, BorderLayout.EAST);
String decodedString = null;
try {
decodedString = new String(decoder.decode(new J2SEImage(sourceImage)));
} catch (DecodingFailedException e) {
canvas.println(e.getMessage());
canvas.println("--------");
return;
}
decodedString = ContentConverter.convert(decodedString);
canvas.println("\nDecode result:");
canvas.println(decodedString);
canvas.println("--------");
if (decodedText != null)
getContentPane().remove(decodedText);
decodedText = new JTextArea(decodedString);
decodedText.setLineWrap(true);
decodedText.setRows(decodedString.length() / 20 + 1);
if (decodedString.length() < 20)
decodedText.setColumns(decodedString.length());
else
decodedText.setColumns(20);
//decodedText.setSize(sourceImageLabel.getSize().width,100);
getContentPane().add(decodedText, BorderLayout.SOUTH);
pack();
}
use of jp.sourceforge.qrcode.exception.DecodingFailedException in project qrcode by yanbe.
the class J2MEImage method cameraCanvasCaptured.
void cameraCanvasCaptured(byte[] pngData) {
cameraCanvas.stop();
displayCanvas.setImage(pngData);
Display.getDisplay(this).setCurrent(displayCanvas);
Image image = Image.createImage(pngData, 0, pngData.length);
// TODO Uncomment below for demo on emulator
try {
image = Image.createImage("/qrcode.jpg");
} catch (IOException ioe) {
}
QRCodeDecoder decoder = new QRCodeDecoder();
QRCodeDecoder.setCanvas(displayCanvas);
try {
decodedTextBox.setDecodedString(new String(decoder.decode(new J2MEImage(image))));
} catch (DecodingFailedException dfe) {
displayCanvas.println("Decoding failed");
displayCanvas.println("(" + dfe.getMessage() + ")");
displayCanvas.println("--------");
return;
}
displayCanvas.println("--------");
displayCanvas.addViewDecodedStringCommand();
}
use of jp.sourceforge.qrcode.exception.DecodingFailedException in project qrcode by yanbe.
the class J2SECanvas method processDecode.
static boolean processDecode(String filename, QRCodeDecoder decoder) {
DebugCanvas canvas = new J2SECanvas();
decoder.setCanvas(canvas);
BufferedImage image = null;
try {
if (filename.startsWith("http://"))
image = ImageIO.read(new URL(filename));
else
image = ImageIO.read(new File(filename));
String decodedString = new String(decoder.decode(new J2SEImage(image)));
decodedString = ContentConverter.convert(decodedString);
System.out.println(decodedString);
} catch (IOException e) {
canvas.println("Error: " + e.getMessage() + " " + filename);
return false;
} catch (DecodingFailedException dfe) {
canvas.println("Error: " + dfe.getMessage());
return false;
} catch (Exception e) {
canvas.println("Error: " + e.getMessage());
return false;
}
return true;
}
use of jp.sourceforge.qrcode.exception.DecodingFailedException in project qrcode by yanbe.
the class JMFCanvas method run.
public void run() {
for (; ; ) {
FrameGrabbingControl fgc = (FrameGrabbingControl) processor.getControl("javax.media.control.FrameGrabbingControl");
Buffer buf = fgc.grabFrame();
BufferToImage btoi = new BufferToImage((VideoFormat) buf.getFormat());
Image img = btoi.createImage(buf);
QRCodeDecoder decoder = new QRCodeDecoder();
int width = processor.getVisualComponent().getWidth();
int height = processor.getVisualComponent().getHeight();
canvas = new JMFCanvas();
QRCodeDecoder.setCanvas(canvas);
try {
J2SEImage decoderImage = new J2SEImage(img, width, height);
String decodedString = new String(decoder.decode(decoderImage));
System.out.println("Result: " + decodedString);
File resultImage = new File("C:\\tmp\\result.jpg");
try {
ImageIO.write(canvas.getImage(), "png", resultImage);
} catch (IOException e) {
System.out.println(e.getMessage());
}
} catch (DecodingFailedException e) {
System.out.println("Error: " + e.getMessage());
} catch (IllegalStateException e) {
System.out.println("Error: " + e.getMessage());
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
Aggregations