1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 文字生成二维码

文字生成二维码

时间:2021-10-11 22:56:29

相关推荐

文字生成二维码

//布局中的实现代码

<RelativeLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.zxing.MainActivity" ><EditText android:id="@+id/editText1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginTop="23dp"android:ems="10" ><requestFocus /></EditText><Button android:id="@+id/button1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:text="生成一个二维码" /><ImageView android:id="@+id/imageView1"android:layout_width="200dp"android:layout_height="200dp"android:layout_centerHorizontal="true"android:layout_centerVertical="true"/></RelativeLayout>

//主类中的实现代码

import com.google.zxing.WriterException;import android.app.Activity;import android.graphics.Bitmap;import android.os.Bundle;import android.util.DisplayMetrics;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;public class MainActivity extends Activity {//定义控件private EditText mEditText;private Button button;private ImageView image;protected int mScreenWidth ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获得控件的id mEditText=(EditText) findViewById(R.id.editText1);button=(Button) findViewById(R.id.button1);image=(ImageView) findViewById(R.id.imageView1);DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);mScreenWidth = dm.widthPixels;button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String string = mEditText.getText().toString();Bitmap bitmap;try {bitmap = BitmapUtil.createQRCode(string, mScreenWidth);if(bitmap != null){image.setImageBitmap(bitmap);}} catch (WriterException e) {e.printStackTrace();}}});}}

现在这个时候会报错因为我们还需要一个工具包

下面展示的就是工具包中的实现代码

import java.util.Hashtable;import android.graphics.Bitmap;import android.util.Log;import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatWriter;import com.google.zxing.WriterException;import com.mon.BitMatrix;import com.google.zxing.qrcode.QRCodeWriter;public class BitmapUtil {/*** 生成一个二维码图像* * @param url* 传入的字符串,通常是一个URL* @param QR_WIDTH* 宽度(像素值px)* @param QR_HEIGHT* 高度(像素值px)* @return*/public static final Bitmap create2DCoderBitmap(String url, int QR_WIDTH,int QR_HEIGHT) {try {// 判断URL合法性if (url == null || "".equals(url) || url.length() < 1) {return null;}Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 图像数据转换,使用了矩阵转换BitMatrix bitMatrix = new QRCodeWriter().encode(url,BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);int[] pixels = new int[QR_WIDTH * QR_HEIGHT];// 下面这里按照二维码的算法,逐个生成二维码的图片,// 两个for循环是图片横列扫描的结果for (int y = 0; y < QR_HEIGHT; y++) {for (int x = 0; x < QR_WIDTH; x++) {if (bitMatrix.get(x, y)) {pixels[y * QR_WIDTH + x] = 0xff000000;} else {pixels[y * QR_WIDTH + x] = 0xffffffff;}}}// 生成二维码图片的格式,使用ARGB_8888Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);// 显示到一个ImageView上面// sweepIV.setImageBitmap(bitmap);return bitmap;} catch (WriterException e) {Log.i("log", "生成二维码错误" + e.getMessage());return null;}}private static final int BLACK = 0xff000000;/*** 生成一个二维码图像* * @param url* 传入的字符串,通常是一个URL* @param widthAndHeight* 图像的宽高* @return*/public static Bitmap createQRCode(String str, int widthAndHeight)throws WriterException {Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");BitMatrix matrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);int width = matrix.getWidth();int height = matrix.getHeight();int[] pixels = new int[width * height];for (int y = 0; y < height; y++) {for (int x = 0; x < width; x++) {if (matrix.get(x, y)) {pixels[y * width + x] = BLACK;}}}Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, width, 0, 0, width, height);return bitmap;}}

这个时候还是会报错 因为导包的问题 所以我们还需要一个扫描二维码的jar包 jar包可以自己下载一下啊

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。