Hey Guys, 

I am using Zxing library to create a QR Code, however to maximize the 
amount of data that can be packed into a QR code, I am compressing the 
data. The issue that I am running into is that I am able to create a QR 
code with the compressed data, however it fails when I try to decompress 
it. I am not sure if I am doing something wrong, or if it is simply not 
possible with QR codes. 

Any assistance will be appreciated, below is my source code. 

public class DataExfil {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        DataExfil dataExfil = new DataExfil();
    }

    public DataExfil() {

        String output = "This Repository contains the source code for the 
QR Code project.\n"
                + "This Repository contains the source code for the QR Code 
project.\n"
                + "This Repository contains the source code for the QR Code 
project.\n"
                + "This Repository contains the source code for the QR Code 
project.\n"
                + "This Repository contains the source code for the QR Code 
project.\n"
                + "This Repository contains the source code for the QR Code 
project.\n"
                + "This Repository contains the source code for the QR Code 
project.\n"
                + "This Repository contains the source code for the QR Code 
project.\n"
                + "This Repository contains the source code for the QR Code 
project.\n"
                + "This Repository contains the source code for the QR Code 
project.\n"
                + "This Repository contains the source code for the QR Code 
project.\n"
                + "This Repository contains the source code for the QR Code 
project.\n"
                + "This Repository contains the source code for the QR Code 
project.\n";
        
        String path = "code.png";
        String doc2;
        String charset = "UTF-8";
        Map map = new HashMap();
        map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);

        try {
            encryptQR(output, path, charset, map, 200, 200);
            System.out.println("No errors occured...");
            System.out.println(decryptQR(path, charset, map));
        } catch (WriterException | IOException | NotFoundException | 
DataFormatException e) {
            // TODO Auto-generated catch block
            Logger.getLogger(DataExfil.class.getName()).log(Level.SEVERE, 
null, e);
        }
    }

    /**
     *
     * @param output
     * @param file
     * @param charset
     * @param hintMap
     * @param width
     * @param height
     * @throws WriterException
     * @throws IOException
     */
    private void encryptQR(String output, String file, String charset,
            Map hintMap, int width, int height) throws WriterException,
            IOException {

        byte[] data;
        data = compress(output.getBytes());

        BitMatrix mat = new MultiFormatWriter().encode(
                new String(data, charset),
                BarcodeFormat.QR_CODE, width, height);
        MatrixToImageWriter.writeToFile(mat,
                file.substring(file.lastIndexOf('.') + 1), new File(file));
    }

    /**
     *
     * @param file
     * @param charset
     * @param map
     * @return
     * @throws IOException
     * @throws NotFoundException
     */
    private String decryptQR(String file, String charset, Map map)
            throws IOException, NotFoundException, DataFormatException {
        BinaryBitmap binBit = new BinaryBitmap(new HybridBinarizer(
                new BufferedImageLuminanceSource(ImageIO.read(new 
File(file)))));
        Result result = new MultiFormatReader().decode(binBit, map);

        byte[] data;
        data = result.getText().getBytes(charset);
        //data = result.getRawBytes();
        byte[] decompressed;
        decompressed = decompress(data);

        String output = new String(decompressed, charset);

        return output;
    }

    /**
     *
     * @param data
     * @return
     * @throws IOException
     */
    public static byte[] compress(byte[] data) throws IOException {
        Deflater deflater = new Deflater();
        deflater.setInput(data);
        deflater.setLevel(Deflater.BEST_SPEED);
        ByteArrayOutputStream outputStream = new 
ByteArrayOutputStream(data.length);
        deflater.finish();
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer); // returns the generated 
code... index  
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
        byte[] output = outputStream.toByteArray();
        System.out.println("Original: " + data.length);
        System.out.println("Compressed: " + output.length);
        return output;
    }

    /**
     *
     * @param data
     * @return
     * @throws IOException
     * @throws DataFormatException
     */
    public static byte[] decompress(byte[] data) throws IOException, 
DataFormatException {
        Inflater inflater = new Inflater();
        inflater.setInput(data);
        ByteArrayOutputStream outputStream = new 
ByteArrayOutputStream(data.length);
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
        byte[] output = outputStream.toByteArray();
        System.out.println("Compressed: " + data.length);
        System.out.println("decompressed: " + output.length);
        return output;
    }
}


-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/66c656ad-70ce-4252-ab2f-c9d0d82c7181%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to