Search in sources :

Example 26 with WriterException

use of com.google.zxing.WriterException in project zxing by zxing.

the class Encoder method interleaveWithECBytes.

/**
   * Interleave "bits" with corresponding error correction bytes. On success, store the result in
   * "result". The interleave rule is complicated. See 8.6 of JISX0510:2004 (p.37) for details.
   */
static BitArray interleaveWithECBytes(BitArray bits, int numTotalBytes, int numDataBytes, int numRSBlocks) throws WriterException {
    // "bits" must have "getNumDataBytes" bytes of data.
    if (bits.getSizeInBytes() != numDataBytes) {
        throw new WriterException("Number of bits and data bytes does not match");
    }
    // Step 1.  Divide data bytes into blocks and generate error correction bytes for them. We'll
    // store the divided data bytes blocks and error correction bytes blocks into "blocks".
    int dataBytesOffset = 0;
    int maxNumDataBytes = 0;
    int maxNumEcBytes = 0;
    // Since, we know the number of reedsolmon blocks, we can initialize the vector with the number.
    Collection<BlockPair> blocks = new ArrayList<>(numRSBlocks);
    for (int i = 0; i < numRSBlocks; ++i) {
        int[] numDataBytesInBlock = new int[1];
        int[] numEcBytesInBlock = new int[1];
        getNumDataBytesAndNumECBytesForBlockID(numTotalBytes, numDataBytes, numRSBlocks, i, numDataBytesInBlock, numEcBytesInBlock);
        int size = numDataBytesInBlock[0];
        byte[] dataBytes = new byte[size];
        bits.toBytes(8 * dataBytesOffset, dataBytes, 0, size);
        byte[] ecBytes = generateECBytes(dataBytes, numEcBytesInBlock[0]);
        blocks.add(new BlockPair(dataBytes, ecBytes));
        maxNumDataBytes = Math.max(maxNumDataBytes, size);
        maxNumEcBytes = Math.max(maxNumEcBytes, ecBytes.length);
        dataBytesOffset += numDataBytesInBlock[0];
    }
    if (numDataBytes != dataBytesOffset) {
        throw new WriterException("Data bytes does not match offset");
    }
    BitArray result = new BitArray();
    // First, place data blocks.
    for (int i = 0; i < maxNumDataBytes; ++i) {
        for (BlockPair block : blocks) {
            byte[] dataBytes = block.getDataBytes();
            if (i < dataBytes.length) {
                result.appendBits(dataBytes[i], 8);
            }
        }
    }
    // Then, place error correction blocks.
    for (int i = 0; i < maxNumEcBytes; ++i) {
        for (BlockPair block : blocks) {
            byte[] ecBytes = block.getErrorCorrectionBytes();
            if (i < ecBytes.length) {
                result.appendBits(ecBytes[i], 8);
            }
        }
    }
    if (numTotalBytes != result.getSizeInBytes()) {
        // Should be same.
        throw new WriterException("Interleaving error: " + numTotalBytes + " and " + result.getSizeInBytes() + " differ.");
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) BitArray(com.google.zxing.common.BitArray) WriterException(com.google.zxing.WriterException)

Example 27 with WriterException

use of com.google.zxing.WriterException in project zxing by zxing.

the class MatrixUtil method makeTypeInfoBits.

// Make bit vector of type information. On success, store the result in "bits" and return true.
// Encode error correction level and mask pattern. See 8.9 of
// JISX0510:2004 (p.45) for details.
static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits) throws WriterException {
    if (!QRCode.isValidMaskPattern(maskPattern)) {
        throw new WriterException("Invalid mask pattern");
    }
    int typeInfo = (ecLevel.getBits() << 3) | maskPattern;
    bits.appendBits(typeInfo, 5);
    int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY);
    bits.appendBits(bchCode, 10);
    BitArray maskBits = new BitArray();
    maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15);
    bits.xor(maskBits);
    if (bits.getSize() != 15) {
        // Just in case.
        throw new WriterException("should not happen but we got: " + bits.getSize());
    }
}
Also used : BitArray(com.google.zxing.common.BitArray) WriterException(com.google.zxing.WriterException)

Example 28 with WriterException

use of com.google.zxing.WriterException in project QRCode by 5peak2me.

the class MainActivity method onClick.

@Override
public void onClick(View v) {
    final int id = v.getId();
    switch(id) {
        case R.id.btn1:
            String content = et.getText().toString().trim();
            try {
                if (!TextUtils.isEmpty(content)) {
                    Bitmap bitmap = Create2DCode(content);
                    iv.setImageBitmap(bitmap);
                } else {
                    Toast.makeText(MainActivity.this, "请输入要生成的字符串", Toast.LENGTH_SHORT).show();
                }
            } catch (WriterException e) {
                e.printStackTrace();
            }
            break;
        case R.id.btn2:
            startActivity(new Intent(MainActivity.this, CaptureActivity.class));
            break;
        default:
            break;
    }
}
Also used : Bitmap(android.graphics.Bitmap) Intent(android.content.Intent) CaptureActivity(com.google.zxing.activity.CaptureActivity) WriterException(com.google.zxing.WriterException)

Example 29 with WriterException

use of com.google.zxing.WriterException in project zxing by zxing.

the class QRCodeEncoder method encodeFromStreamExtra.

// Handles send intents from the Contacts app, retrieving a contact as a VCARD.
private void encodeFromStreamExtra(Intent intent) throws WriterException {
    format = BarcodeFormat.QR_CODE;
    Bundle bundle = intent.getExtras();
    if (bundle == null) {
        throw new WriterException("No extras");
    }
    Uri uri = bundle.getParcelable(Intent.EXTRA_STREAM);
    if (uri == null) {
        throw new WriterException("No EXTRA_STREAM");
    }
    byte[] vcard;
    String vcardString;
    InputStream stream = null;
    try {
        stream = activity.getContentResolver().openInputStream(uri);
        if (stream == null) {
            throw new WriterException("Can't open stream for " + uri);
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[2048];
        int bytesRead;
        while ((bytesRead = stream.read(buffer)) > 0) {
            baos.write(buffer, 0, bytesRead);
        }
        vcard = baos.toByteArray();
        vcardString = new String(vcard, 0, vcard.length, "UTF-8");
    } catch (IOException ioe) {
        throw new WriterException(ioe);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
            // continue
            }
        }
    }
    Log.d(TAG, "Encoding share intent content:");
    Log.d(TAG, vcardString);
    Result result = new Result(vcardString, vcard, null, BarcodeFormat.QR_CODE);
    ParsedResult parsedResult = ResultParser.parseResult(result);
    if (!(parsedResult instanceof AddressBookParsedResult)) {
        throw new WriterException("Result was not an address");
    }
    encodeQRCodeContents((AddressBookParsedResult) parsedResult);
    if (contents == null || contents.isEmpty()) {
        throw new WriterException("No content to encode");
    }
}
Also used : AddressBookParsedResult(com.google.zxing.client.result.AddressBookParsedResult) Bundle(android.os.Bundle) InputStream(java.io.InputStream) ParsedResult(com.google.zxing.client.result.ParsedResult) AddressBookParsedResult(com.google.zxing.client.result.AddressBookParsedResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Uri(android.net.Uri) WriterException(com.google.zxing.WriterException) Result(com.google.zxing.Result) ParsedResult(com.google.zxing.client.result.ParsedResult) AddressBookParsedResult(com.google.zxing.client.result.AddressBookParsedResult)

Example 30 with WriterException

use of com.google.zxing.WriterException in project wechat by motianhuo.

the class MyCodeActivity method generateQRCode.

private Bitmap generateQRCode(String content) {
    try {
        QRCodeWriter writer = new QRCodeWriter();
        // MultiFormatWriter writer = new MultiFormatWriter();
        BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, 500, 500);
        return bitMatrix2Bitmap(matrix);
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : QRCodeWriter(com.google.zxing.qrcode.QRCodeWriter) BitMatrix(com.google.zxing.common.BitMatrix) WriterException(com.google.zxing.WriterException)

Aggregations

WriterException (com.google.zxing.WriterException)32 Bitmap (android.graphics.Bitmap)14 BitMatrix (com.google.zxing.common.BitMatrix)10 QRCodeWriter (com.google.zxing.qrcode.QRCodeWriter)10 IOException (java.io.IOException)8 Intent (android.content.Intent)7 EncodeHintType (com.google.zxing.EncodeHintType)7 BitArray (com.google.zxing.common.BitArray)7 FileOutputStream (java.io.FileOutputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Hashtable (java.util.Hashtable)4 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