Generate designer 2d QR code in android

ぃ、小莉子 提交于 2020-01-01 03:45:05

问题


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 ?


回答1:


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>

To generate and display a QR-encoded image use code like 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 build.gradle file:

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

Or - if still using Eclipse with ADT plugin add core.jar by ZXing to the libs subdir (here fullscreen):




回答2:


Personally I use this library

Thats how you can generate QR code with that

Java

Bitmap myBitmap = QRCode.from("www.example.org").bitmap();
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);

Kotlin

val qrImage = findViewById<ImageView>(R.id.img_qr_code)
val myBitmap = QRCode.from("www.example.org").bitmap()
qrImage.setImageBitmap(myBitmap)



回答3:


For whose ineteresting in the same, explore this project

This is not a code of mine, but I've checked it and it works fine.

The main idea is in QRCodeUtil. It's just simple overlay. Unfortunately, no theory limitation provided.

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


来源:https://stackoverflow.com/questions/28827407/generate-designer-2d-qr-code-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!