I need to create a qrcode in my android application, and I need a library or source code that lets me create a QR Code in an Android app.
我需要在我的android应用程序中创建一个qrcode,我需要一个库或源代码,让我在android应用程序中创建二维码。
The library I need must:
我需要的图书馆必须:
- not leave a watermark (like
onbarcode
library) - 不留下水印(如onbarcode库)
- not use web service API to create the qrcode (like Google's library zxing)
- 不使用web服务API创建qrcode(如谷歌的库zxing)
- not need 3rd party installers (like QR Droid)
- 不需要第三方安装程序(比如QR Droid)
I already created such code for iPhone (Objective-C) but I need a quick fix for Android until I have time to make a QR Code generator of my own. It's my first android project so any help will be appreciated.
我已经为iPhone (Objective-C)创建了这样的代码,但我需要对Android进行快速修复,直到我有时间制作自己的二维码生成器。这是我的第一个android项目,所以任何帮助都会受到感激。
6 个解决方案
#1
49
Have you looked into ZXING? I've been using it successfully to create barcodes. You can see a full working example in the bitcoin application src
你调查过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 }
#2
57
with zxing this is my code for create QR
zxing这是我的二维码
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();
}
#3
15
Maybe this old topic but i found this library is very helpful and easy to use
也许这个老话题,但是我发现这个库很有用,很容易使用
QRGen
example for using it in android
在android中使用它的例子
Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);
#4
11
Here is my simple and working function to generate a Bitmap! I Use ZXing1.3.jar only! I've also set Correction Level to High!
这是我生成位图的简单而有效的函数!我使用ZXing1.3。罐子!我也把校正水平设置到高!
PS: x and y are reversed, it's normal, because bitMatrix reverse x and y. This code works perfectly with a square image.
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;
}
EDIT
编辑
It's faster to use bitmap.setPixels(...) with a pixel int array instead of bitmap.setPixel one by one:
使用位图. setpixels(…)使用像素int数组而不是位图会更快。setPixel一个接一个:
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);
#5
7
I used zxing-1.3 jar and I had to make some changes implementing code from other answers, so I will leave my solution for others. I did the following:
我使用了zxt -1.3 jar,并且必须对实现其他答案的代码做一些更改,所以我将把我的解决方案留给其他人。我做了以下几点:
1) find zxing-1.3.jar, download it and add in properties (add external jar).
1)找到zx - 1.3。jar,下载并添加属性(添加外部jar)。
2) in my activity layout add ImageView and name it (in my example it was tnsd_iv_qr).
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):
3)在我创建二维码的活动中包含代码(在本例中,我为比特币支付创建二维码):
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.
如果有人想知道,变量“btc_acc_adress”是一个字符串(带有BTC adress), amountBTC是一个双精度的,当然,还有事务量。
#6
5
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.
zxing(仅)不提供web API;实际上,这是谷歌提供的API,来自后来项目中开放源代码的源代码。
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.
正如Rob所说,您可以使用二维码编码器的Java源代码来创建原始条形码,然后将其呈现为位图。
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()
.
我可以提供更简单的方法。您可以调用条形码扫描器来编码条形码。在android-integration下,您只需要几行代码,以及来自项目的两个类。最主要的是集成电路。就叫shareText()。
#1
49
Have you looked into ZXING? I've been using it successfully to create barcodes. You can see a full working example in the bitcoin application src
你调查过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 }
#2
57
with zxing this is my code for create QR
zxing这是我的二维码
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();
}
#3
15
Maybe this old topic but i found this library is very helpful and easy to use
也许这个老话题,但是我发现这个库很有用,很容易使用
QRGen
example for using it in android
在android中使用它的例子
Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);
#4
11
Here is my simple and working function to generate a Bitmap! I Use ZXing1.3.jar only! I've also set Correction Level to High!
这是我生成位图的简单而有效的函数!我使用ZXing1.3。罐子!我也把校正水平设置到高!
PS: x and y are reversed, it's normal, because bitMatrix reverse x and y. This code works perfectly with a square image.
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;
}
EDIT
编辑
It's faster to use bitmap.setPixels(...) with a pixel int array instead of bitmap.setPixel one by one:
使用位图. setpixels(…)使用像素int数组而不是位图会更快。setPixel一个接一个:
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);
#5
7
I used zxing-1.3 jar and I had to make some changes implementing code from other answers, so I will leave my solution for others. I did the following:
我使用了zxt -1.3 jar,并且必须对实现其他答案的代码做一些更改,所以我将把我的解决方案留给其他人。我做了以下几点:
1) find zxing-1.3.jar, download it and add in properties (add external jar).
1)找到zx - 1.3。jar,下载并添加属性(添加外部jar)。
2) in my activity layout add ImageView and name it (in my example it was tnsd_iv_qr).
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):
3)在我创建二维码的活动中包含代码(在本例中,我为比特币支付创建二维码):
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.
如果有人想知道,变量“btc_acc_adress”是一个字符串(带有BTC adress), amountBTC是一个双精度的,当然,还有事务量。
#6
5
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.
zxing(仅)不提供web API;实际上,这是谷歌提供的API,来自后来项目中开放源代码的源代码。
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.
正如Rob所说,您可以使用二维码编码器的Java源代码来创建原始条形码,然后将其呈现为位图。
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()
.
我可以提供更简单的方法。您可以调用条形码扫描器来编码条形码。在android-integration下,您只需要几行代码,以及来自项目的两个类。最主要的是集成电路。就叫shareText()。