安卓开发中使用ZXing生成解析二维码
Posted 布伊什
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓开发中使用ZXing生成解析二维码相关的知识,希望对你有一定的参考价值。
编码示例
package com.wolf_pan.qrcodesample; import android.graphics.Bitmap; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import java.util.HashMap; public class EncodingSampleActivity extends AppCompatActivity { private LinearLayout encodingSampleLinearLayout; private EditText textToEncodeEditText; private ImageView encodedImageView; private final String tag = "EncodingSampleActivity"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_encoding_sample); encodingSampleLinearLayout = findViewById(R.id.encodingSampleLinearLayout); textToEncodeEditText = findViewById(R.id.textToEncodeEditText); encodedImageView = findViewById(R.id.encodedImageView); findViewById(R.id.encodeButton).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String contents = textToEncodeEditText.getText().toString(); if (contents.isEmpty()) contents = "在此输入要编码的文本"; try { int width = encodingSampleLinearLayout.getWidth() - 50, height = width; HashMap<EncodeHintType, String> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new QRCodeWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints); int[] colors = new int[width * height]; for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) colors[width * y + x] = bitMatrix.get(x, y) ? Color.RED : Color.WHITE; } Bitmap qrcodeBitmap = Bitmap.createBitmap(colors, width, height, Bitmap.Config.RGB_565); encodedImageView.setImageBitmap(qrcodeBitmap); } catch (WriterException ex) { Log.e(tag, "ex: " + ex); } } }); } }
解码示例
package com.wolf_pan.qrcodesample; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import com.google.zxing.BinaryBitmap; import com.google.zxing.RGBLuminanceSource; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.QRCodeReader; import java.io.IOException; public class DecodingSampleActivity extends AppCompatActivity { public ImageView qrcodeToDecodeImageView; public TextView decodedTextView; private Bitmap bitmap; private final String tag = "DecodingSampleActivity"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_decoding_sample); qrcodeToDecodeImageView = findViewById(R.id.qrcodeToDecodeImageView); decodedTextView = findViewById(R.id.decodedTextView); findViewById(R.id.decodeButton).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { int width = bitmap.getWidth(), height = bitmap.getHeight(); int[] pixels = new int[width * height]; bitmap.getPixels(pixels, 0, width, 0, 0, width, height); String decoded = new QRCodeReader().decode(new BinaryBitmap(new HybridBinarizer(new RGBLuminanceSource(width, height, pixels)))).getText(); decodedTextView.setText(decoded); } catch (Exception ex) { Log.e(tag, "ex: " + ex); } } }); try { bitmap = BitmapFactory.decodeStream(getAssets().open("qrcode_qq.png")); qrcodeToDecodeImageView.setImageBitmap(bitmap); } catch (IOException ex) { Log.e(tag, "ex: " + ex); } } }
以上是关于安卓开发中使用ZXing生成解析二维码的主要内容,如果未能解决你的问题,请参考以下文章
java zxing实现二维码生成和解析zxing实现二维码生成和解析