Search in sources :

Example 36 with WriterException

use of com.google.zxing.WriterException in project run-wallet-android by runplay.

the class QR method generateImage.

public static void generateImage(final String text, final ImageView showView, Activity activity) {
    if (text.trim().isEmpty()) {
        // alert("Ketik dulu data yang ingin dibuat QR Code");
        return;
    }
    // endEditing();
    // showLoadingVisible(true);
    new Thread(new Runnable() {

        @Override
        public void run() {
            int size = showView.getMeasuredWidth();
            if (size > 1) {
                // Log.e("QR", "size is set manually");
                size = 260;
            }
            Map<EncodeHintType, Object> hintMap = new EnumMap<>(EncodeHintType.class);
            hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            hintMap.put(EncodeHintType.MARGIN, 1);
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            try {
                BitMatrix byteMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, size, size, hintMap);
                int height = byteMatrix.getHeight();
                int width = byteMatrix.getWidth();
                final Bitmap qrImage = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
                for (int x = 0; x < width; x++) {
                    for (int y = 0; y < height; y++) {
                        qrImage.setPixel(x, y, byteMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
                    }
                }
                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        showView.setImageBitmap(qrImage);
                    // activity.showImage(self.qrImage);
                    // self.showLoadingVisible(false);
                    // self.snackbar("QRCode telah dibuat");
                    }
                });
            } catch (WriterException e) {
                Log.e("QR.ex", "" + e.getMessage());
            }
        }
    }).start();
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) Bitmap(android.graphics.Bitmap) EncodeHintType(com.google.zxing.EncodeHintType) BitMatrix(com.google.zxing.common.BitMatrix) EnumMap(java.util.EnumMap) Paint(android.graphics.Paint) WriterException(com.google.zxing.WriterException)

Aggregations

WriterException (com.google.zxing.WriterException)36 Bitmap (android.graphics.Bitmap)17 BitMatrix (com.google.zxing.common.BitMatrix)14 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)12 EncodeHintType (com.google.zxing.EncodeHintType)10 IOException (java.io.IOException)9 Intent (android.content.Intent)7 BitArray (com.google.zxing.common.BitArray)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 FileOutputStream (java.io.FileOutputStream)5 Hashtable (java.util.Hashtable)5 Point (android.graphics.Point)3 Uri (android.net.Uri)3 Bundle (android.os.Bundle)3 Display (android.view.Display)3 WindowManager (android.view.WindowManager)3 ImageView (android.widget.ImageView)3 TextView (android.widget.TextView)3 Result (com.google.zxing.Result)3 AddressBookParsedResult (com.google.zxing.client.result.AddressBookParsedResult)3