设计师生成二维QR code在安卓 [英] Generate designer 2d QR code in android

查看:211
本文介绍了设计师生成二维QR code在安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何生成的二维QR $ C $下一些文字,并具有图像的中心机器人?我浏览了很多,但我只发现了如何通过使用此的链接。是否有可能生成具有图像为中心的使用ZXing库2-D二维code?

How to generate 2-d QR Code for some text and also having an image at centre in android ? I browse a lot but i found only how to generate simple 2-d QR code using ZXing library using this link. Is it possible to generate 2-d QR Code having an image at center using ZXing library ?

推荐答案

要居中对齐的图像使用code就像在我的 activity_main.xml

To center-align an image use code like 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>

要生成,在我的<显示器的QR-CN codeD映像使用code状href="https://github.com/afarber/android-newbie/blob/master/QREn$c$cr/src/de/afarber/qren$c$cr/MainActivity.java"相对=nofollow> 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;
    }
}

还记得添加的 core.jar添加的由ZXing到的的你的子目录Eclipse的ADT项目(这里的全屏):

Also remember to add core.jar by ZXing to the libs subdir of your Eclipse-ADT project (here fullscreen):

这篇关于设计师生成二维QR code在安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆