如何为某些文本生成二维QR码,并在android中心生成一个图像?我浏览了很多,但我发现如何使用这个link使用ZXing库生成简单的二维QR码。是否可以使用ZXing库生成具有中心图像的二维QR码?
答案 0 :(得分:7)
使用activity_main.xml中的图像使用代码居中对齐:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/myImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
要生成并显示QR编码的图像,请使用我MainActivity.java中的代码:
public class MainActivity extends AppCompatActivity {
public final static int WHITE = 0xFFFFFFFF;
public final static int BLACK = 0xFF000000;
public final static int WIDTH = 400;
public final static int HEIGHT = 400;
public final static String STR = "A string to be encoded as QR code";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.myImage);
try {
Bitmap bitmap = encodeAsBitmap(STR);
imageView.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}
Bitmap encodeAsBitmap(String str) throws WriterException {
BitMatrix result;
try {
result = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, WIDTH, HEIGHT, null);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;
}
int w = result.getWidth();
int h = result.getHeight();
int[] pixels = new int[w * h];
for (int y = 0; y < h; y++) {
int offset = y * w;
for (int x = 0; x < w; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, w, 0, 0, w, h);
return bitmap;
}
}
在 Android Studio 中,将以下行添加到build.gradle
文件中:
dependencies {
....
compile 'com.google.zxing:core:3.2.1'
}
或者 - 如果仍然使用带有ADT插件的 Eclipse ,请将ZXing的 core.jar 添加到 libs 子目录(此处为fullscreen ):
答案 1 :(得分:0)
我个人使用此library
这就是您可以用来生成QR码的方式
Java
tfTitle.requestFocusInWindow();
taDesc.setLineWrap(true);
taDesc.setWrapStyleWord(true);
overlayPanel.setBackground(new Color(20,20,20, 150));
overlayPanel.setVisible(false);
layeredPane2.moveToFront(popupPanel);
layeredPane2.moveToBack(backPanel);
popupPanel.setVisible(false);
科特琳
Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);
答案 2 :(得分:-1)
对于那些不感兴趣的人,请探索this project
这不是我的代码,但我已经检查过它并且工作正常。
主要思想是 QRCodeUtil 。它只是简单的叠加。不幸的是,没有提供理论限制。
private static Bitmap addLogo(Bitmap src, Bitmap logo) {
if (src == null) {
return null;
}
if (logo == null) {
return src;
}
//获取图片的宽高
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
int logoWidth = logo.getWidth();
int logoHeight = logo.getHeight();
if (srcWidth == 0 || srcHeight == 0) {
return null;
}
if (logoWidth == 0 || logoHeight == 0) {
return src;
}
//logo大小为二维码整体大小的1/5
float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
try {
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(src, 0, 0, null);
canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
} catch (Exception e) {
bitmap = null;
e.getStackTrace();
}
return bitmap;
}