2015-03-03 52 views
2

如何生成一些文本的二维QR码,并且在android中有一个图像在中心?我浏览了很多,但是我只找到了如何使用这个link使用ZXing库来生成简单的2-D QR码。使用ZXing库可以生成具有中心图像的二维QR码吗?在android中生成设计器2d QR码

回答

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> 

app screenshot

要生成并在显示一个类似的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' 
} 

或者 - 如果仍然使用使用的Eclipse ADT插件通过斑马线到子目录(这里fullscreen)添加core.jar添加

Eclipse screenshot

+0

亚历克斯,TS被问及-QR图像,而不是如何中心的图像 – djdance 2017-03-09 18:00:18

+1

不是我,而是TS - 谈到叠加图像,这里是一个正确的观念https://github.com/wshychbydh/Qrcode – djdance 2017-03-10 07:15:22

-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; 
}