Search in sources :

Example 1 with DecodingFailedException

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());
    }
}
Also used : QRCodeImageReader(jp.sourceforge.qrcode.reader.QRCodeImageReader) InvalidDataBlockException(jp.sourceforge.qrcode.exception.InvalidDataBlockException) SymbolNotFoundException(jp.sourceforge.qrcode.exception.SymbolNotFoundException) DecodingFailedException(jp.sourceforge.qrcode.exception.DecodingFailedException)

Example 2 with DecodingFailedException

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();
}
Also used : DecodingFailedException(jp.sourceforge.qrcode.exception.DecodingFailedException) URL(java.net.URL) DecodingFailedException(jp.sourceforge.qrcode.exception.DecodingFailedException)

Example 3 with DecodingFailedException

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();
}
Also used : IOException(java.io.IOException) QRCodeImage(jp.sourceforge.qrcode.data.QRCodeImage) Image(javax.microedition.lcdui.Image) QRCodeDecoder(jp.sourceforge.qrcode.QRCodeDecoder) DecodingFailedException(jp.sourceforge.qrcode.exception.DecodingFailedException)

Example 4 with DecodingFailedException

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;
}
Also used : IOException(java.io.IOException) DebugCanvas(jp.sourceforge.qrcode.util.DebugCanvas) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) URL(java.net.URL) DecodingFailedException(jp.sourceforge.qrcode.exception.DecodingFailedException) IOException(java.io.IOException) InvalidVersionInfoException(jp.sourceforge.qrcode.exception.InvalidVersionInfoException) DecodingFailedException(jp.sourceforge.qrcode.exception.DecodingFailedException)

Example 5 with DecodingFailedException

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());
        }
    }
}
Also used : Buffer(javax.media.Buffer) IOException(java.io.IOException) FrameGrabbingControl(javax.media.control.FrameGrabbingControl) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) QRCodeImage(jp.sourceforge.qrcode.data.QRCodeImage) BufferToImage(javax.media.util.BufferToImage) BufferToImage(javax.media.util.BufferToImage) Point(jp.sourceforge.qrcode.geom.Point) File(java.io.File) QRCodeDecoder(jp.sourceforge.qrcode.QRCodeDecoder) DecodingFailedException(jp.sourceforge.qrcode.exception.DecodingFailedException)

Aggregations

DecodingFailedException (jp.sourceforge.qrcode.exception.DecodingFailedException)6 IOException (java.io.IOException)3 BufferedImage (java.awt.image.BufferedImage)2 File (java.io.File)2 URL (java.net.URL)2 QRCodeDecoder (jp.sourceforge.qrcode.QRCodeDecoder)2 QRCodeImage (jp.sourceforge.qrcode.data.QRCodeImage)2 Point (jp.sourceforge.qrcode.geom.Point)2 Image (java.awt.Image)1 Vector (java.util.Vector)1 Buffer (javax.media.Buffer)1 FrameGrabbingControl (javax.media.control.FrameGrabbingControl)1 BufferToImage (javax.media.util.BufferToImage)1 Image (javax.microedition.lcdui.Image)1 InvalidDataBlockException (jp.sourceforge.qrcode.exception.InvalidDataBlockException)1 InvalidVersionInfoException (jp.sourceforge.qrcode.exception.InvalidVersionInfoException)1 SymbolNotFoundException (jp.sourceforge.qrcode.exception.SymbolNotFoundException)1 QRCodeImageReader (jp.sourceforge.qrcode.reader.QRCodeImageReader)1 DebugCanvas (jp.sourceforge.qrcode.util.DebugCanvas)1