Create designer 2D code in Android

How to generate a 2nd QR code for some text, and also have an image in the center in android? I read a lot, but only found how to generate a simple 2nd QR code using the ZXing library using this link. Is it possible to generate a 2nd QR code with the image in the center using the ZXing library?

+2
android qr-code
03 Mar. '15 at 8:45
source share
2 answers

To center the image usage code, as in my 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> 

app screenshot

To generate and display a QR code usage code, as in my 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; } } 

In Android Studio, add the following line to the build.gradle file:

 dependencies { .... compile 'com.google.zxing:core:3.2.1' } 

Or - if you still use Eclipse with the ADT plugin , add core.jar ZXing to the libs subdirectory (here fullscreen ):

Eclipse screenshot

+6
May 29 '15 at 12:22
source share

For those who do not understand this, explore this project.

This is not my code, but I checked it and it works great.

The basic idea is QRCodeUtil . This is just an overlay. Unfortunately, there were no limitations of the theory.

  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; } 
-one
Mar 10 '17 at 7:18
source share



All Articles