在android中生成设计师2d QR码

Rah*_*pta 2 android qr-code

如何为某些文本生成二维QR码,并在android中心生成一个图像?我浏览了很多,但我发现如何使用此链接使用ZXing库生成简单的二维QR码.是否可以使用ZXing库生成具有中心图像的二维QR码?

Ale*_*ber 9

要像我的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>
Run Code Online (Sandbox Code Playgroud)

应用截图

要生成和显示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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Android Studio中,将以下行添加到build.gradle文件中:

dependencies {
    ....
    compile 'com.google.zxing:core:3.2.1'
}
Run Code Online (Sandbox Code Playgroud)

或者 - 如果仍然使用带有ADT插件的Eclipse,则将ZXing的core.jar添加到libs子目录(此处为全屏):

Eclipse截图