[英]How to encode text with greek letters in a QR Code in android studio
所以我正在嘗試制作一個二維碼,掃描時它會返回一個文本。 這是代碼。
//GenerateCode is a button
GenerateCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = PlayerAddEditText.getText().toString(); //PlayerAddEditText is an editText
try {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
BitMatrix bitMatrix = multiFormatWriter.encode(name, BarcodeFormat.QR_CODE, 200, 200);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
QRCodeImageView.setImageBitmap(bitmap); //QRCodeImageView is an imageview
} catch (WriterException e) {
e.printStackTrace();
}
}
});
使用英文字母一切正常,但是當我在 editText 中輸入希臘字母並運行代碼時,結果字符串返回問號。 我嘗試使用在線二維碼生成器來查看問題是否出在我掃描二維碼時,但它與在線二維碼一起工作正常,所以我認為問題是我的二維碼生成器代碼。但這是獲取結果的代碼(掃描二維碼) .
//ScanCode is a button
ScanCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ScanCodeOnClick();
}
});
}
public void ScanCodeOnClick() {
Dexter.withActivity(this)
.withPermission(Manifest.permission.CAMERA)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
ScanQRView.setResultHandler(AddPlayerActivity.this); // ScanQRView its a view that the camera is show(Instead of opening camera activity)
ScanQRView.startCamera();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
toastMake("Πρέπει να πατήσεται αποδοχή"); //Translation:You have to accept the Permission
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
}
})
.check();
}
@Override
protected void onDestroy() {
ScanQRView.stopCamera();
super.onDestroy();
}
@Override
public void handleResult(Result rawResult) {
QRPlayerValues = rawResult.getText(); //QRPlayerValues is the result string;
PlayerAddEditText.setText(QRPlayerValues);
ScanQRView.startCamera();
}
public void toastMake(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
我正在使用 2 部手機在同一個應用程序中執行此操作,但我有一個要生成的按鈕和一個要掃描的按鈕(僅供說明)。當用戶輸入希臘字母時,我需要能夠掃描並獲取希臘字母。
我用它來寫俄文字母,我想它也適用於你的情況。 在行中設置字符集hints.put(EncodeHintType.CHARACTER_SET, "Cp1251");
:
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.datamatrix.encoder.SymbolShapeHint;
import com.google.zxing.qrcode.QRCodeWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Hashtable;
/**
* Created by aprisadkov on 13.03.2019.
*/
public class QRCodeHelper {
public static byte[] generateImage(String contents) throws WriterException, IOException {
return generateImage(contents, 256);
}
public static byte[] generateImage(String contents, int size) throws WriterException, IOException {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "Cp1251");
hints.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_RECTANGLE);
BitMatrix bitMatrix = qrCodeWriter.encode(contents, BarcodeFormat.QR_CODE, size, size, hints);
ByteArrayOutputStream w = new ByteArrayOutputStream(size*size);
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", w);
return w.toByteArray();
}
}
更新:您也可以在BitMatrix bitMatrix = multiFormatWriter.encode(name, BarcodeFormat.QR_CODE, 200, 200);
行中使用提示BitMatrix bitMatrix = multiFormatWriter.encode(name, BarcodeFormat.QR_CODE, 200, 200);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.