Android 애플리케이션 용 QR 코드를 생성하는 방법은 무엇입니까?
Android 애플리케이션에서 qrcode를 생성해야하고 Android 앱에서 QR 코드를 생성 할 수있는 라이브러리 또는 소스 코드가 필요합니다.
필요한 라이브러리는 다음과 같아야합니다.
- 워터 마크를 남기지 마십시오 (예 :
onbarcode라이브러리) - 웹 서비스 API를 사용하여 qrcode를 생성하지 마십시오 (예 : Google의 라이브러리 zxing).
- 타사 설치 프로그램 (예 : QR Droid)이 필요하지 않습니다.
이미 iPhone (Objective-C) 용 코드를 만들었지 만 QR 코드 생성기를 직접 만들 시간이 생길 때까지 Android 용 빠른 수정이 필요합니다. 내 첫 번째 안드로이드 프로젝트이므로 어떤 도움을 주시면 감사하겠습니다.
ZXING 을 살펴 보셨습니까 ? 바코드를 만드는 데 성공적으로 사용하고 있습니다. 비트 코인 애플리케이션 src 에서 전체 작동 예제를 볼 수 있습니다.
// this is a small sample use of the QRCodeEncoder class from zxing
try {
// generate a 150x150 QR code
Bitmap bm = encodeAsBitmap(barcode_content, BarcodeFormat.QR_CODE, 150, 150);
if(bm != null) {
image_view.setImageBitmap(bm);
}
} catch (WriterException e) { //eek }
zxing으로 이것은 QR 생성을위한 내 코드입니다.
QRCodeWriter writer = new QRCodeWriter();
try {
BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, 512, 512);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
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, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
((ImageView) findViewById(R.id.img_result_qr)).setImageBitmap(bmp);
} catch (WriterException e) {
e.printStackTrace();
}
이 오래된 주제 일 수도 있지만이 라이브러리는 매우 유용하고 사용하기 쉽습니다.
Android에서 사용하는 예
Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);
다음은 비트 맵을 생성하는 간단하고 작동하는 함수입니다! ZXing1.3.jar 만 사용합니다! 수정 수준도 높음으로 설정했습니다!
추신 : bitMatrix가 x와 y를 반대로하기 때문에 x와 y가 반대로되어 정상입니다. 이 코드는 정사각형 이미지에서 완벽하게 작동합니다.
public static Bitmap generateQrCode(String myCodeText) throws WriterException {
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // H = 30% damage
QRCodeWriter qrCodeWriter = new QRCodeWriter();
int size = 256;
ByteMatrix bitMatrix = qrCodeWriter.encode(myCodeText,BarcodeFormat.QR_CODE, size, size, hintMap);
int width = bitMatrix.width();
Bitmap bmp = Bitmap.createBitmap(width, width, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++) {
for (int y = 0; y < width; y++) {
bmp.setPixel(y, x, bitMatrix.get(x, y)==0 ? Color.BLACK : Color.WHITE);
}
}
return bmp;
}
편집하다
bitmap.setPixel 대신 픽셀 int 배열과 함께 bitmap.setPixels (...)를 하나씩 사용하는 것이 더 빠릅니다.
BitMatrix bitMatrix = writer.encode(inputValue, BarcodeFormat.QR_CODE, size, size);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = bitMatrix.get(x, y) ? BLACK : WHITE;
}
}
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
나는 zxing-1.3 jar를 사용했고 다른 답변의 코드를 구현하는 몇 가지 변경을해야 했으므로 다른 사람들에게 내 솔루션을 남겨 둘 것입니다. 다음을 수행했습니다.
1) zxing-1.3.jar을 찾아 다운로드하고 속성을 추가합니다 (외부 jar 추가).
2) 내 활동 레이아웃에서 ImageView를 추가하고 이름을 지정합니다 (내 예에서는 tnsd_iv_qr).
3) include code in my activity to create qr image (in this example I was creating QR for bitcoin payments):
QRCodeWriter writer = new QRCodeWriter();
ImageView tnsd_iv_qr = (ImageView)findViewById(R.id.tnsd_iv_qr);
try {
ByteMatrix bitMatrix = writer.encode("bitcoin:"+btc_acc_adress+"?amount="+amountBTC, BarcodeFormat.QR_CODE, 512, 512);
int width = 512;
int height = 512;
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (bitMatrix.get(x, y)==0)
bmp.setPixel(x, y, Color.BLACK);
else
bmp.setPixel(x, y, Color.WHITE);
}
}
tnsd_iv_qr.setImageBitmap(bmp);
} catch (WriterException e) {
//Log.e("QR ERROR", ""+e);
}
If someone is wondering, variable "btc_acc_adress" is a String (with BTC adress), amountBTC is a double, with, of course, transaction amount.
zxing does not (only) provide a web API; really, that is Google providing the API, from source code that was later open-sourced in the project.
As Rob says here you can use the Java source code for the QR code encoder to create a raw barcode and then render it as a Bitmap.
I can offer an easier way still. You can call Barcode Scanner by Intent to encode a barcode. You need just a few lines of code, and two classes from the project, under android-integration. The main one is IntentIntegrator. Just call shareText().
참고URL : https://stackoverflow.com/questions/8800919/how-to-generate-a-qr-code-for-an-android-application
'Programing' 카테고리의 다른 글
| jQuery를 사용하지 않는 경험적 기술적 이유는 무엇입니까? (0) | 2020.10.23 |
|---|---|
| android.jar에 소스를 첨부하는 방법 (0) | 2020.10.23 |
| 16 진수 색상 값 (#ffffff)을 정수 값으로 변환 (0) | 2020.10.23 |
| 입력 유형 =“number”에 대한 onchange 이벤트 (0) | 2020.10.23 |
| Swift에서 관련 값을 무시하여 enum을 관련 값과 비교하는 방법은 무엇입니까? (0) | 2020.10.22 |