2017-09-26 19 views
1

にコード化されたQRコードからbyte[] arrayを取得する必要があります。 QRコードを生成するためにQRコードから生のバイトをzxing libで取得する(またはBitMatrixから変換する)

// imports 
import com.google.zxing.BarcodeFormat; 
import com.google.zxing.ChecksumException; 
import com.google.zxing.FormatException; 
import com.google.zxing.Writer; 
import com.google.zxing.WriterException; 
import com.google.zxing.common.BitMatrix; 
import com.google.zxing.common.DecoderResult; 
import com.google.zxing.qrcode.QRCodeWriter; 
import com.google.zxing.datamatrix.decoder.Decoder; 

機能:

public byte[] createQRCode() { 
    String qrCodeData = "Hello world"; 
    String charset = "UTF-8"; 
    BitMatrix matrix = null; 
    Writer writer = new QRCodeWriter(); 

    try { 
     matrix = writer.encode(new String(qrCodeData.getBytes(charset), charset), BarcodeFormat.QR_CODE, qrCodeheight, qrCodewidth); 
    } catch (UnsupportedEncodingException e) { 
     return; 
    } 
    catch (WriterException e) { 
     return; 
    } 

    DecoderResult decoderResult = null; 
    try { 
     decoderResult = new Decoder().decode(matrix); 
    } catch (ChecksumException e) { 
     return; 
    } catch (FormatException e) { 
     // Always this exception is throwed 
    } 

    byte[] cmd = decoderResult.getRawBytes();` 
    return cmd; 
} 

が常に要求された Decode().decode()上でもパラメータ FormatExceptionの実行停止は、 BitMatrixである。ここに私のコードです。

QRコードbyteアレイを入手するには、何が間違っているか教えてください。

答えて

0

私は、ビットマップをデコードするためのライブラリを使用して解決策を見つけた:QRコードビットマップに文字列をエンコードする https://github.com/imrankst1221/Thermal-Printer-in-Android

機能:

public Bitmap encodeToQrCode(String text, int width, int height){ 
    QRCodeWriter writer = new QRCodeWriter(); 
    BitMatrix matrix = null; 
    try { 
     matrix = writer.encode(text, BarcodeFormat.QR_CODE, width, height); 
    } catch (WriterException ex) { 
     // 
    } 
    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); 
    for (int x = 0; x < width; x++){ 
     for (int y = 0; y < height; y++){ 
      bmp.setPixel(x, y, matrix.get(x,y) ? Color.BLACK : Color.WHITE); 
     } 
    } 
    return bmp; 
} 

を次に私が見つけたライブラリーからのUtilsを使用してバイトにビットマップをデコード:

try { 
    Bitmap bmp = encodeToQrCode("Hello world", 200, 200); 
    if (bmp != null) { 
     byte[] command = Utils.decodeBitmap(bmp); 
     BluetoothPrintDriver.BT_Write(command); 
    }else{ 
     Log.e("Print Photo error", "the file isn't exists"); 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
    Log.e("PrintTools", "the file isn't exists"); 
} 
関連する問題