android生成二维码实例
生成二维码工具类
package com.catanddog.utils;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.TextUtils;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
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.common.CharacterSetECI;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.util.Hashtable;
/**
* @description: 二维码工具类
* @create: 2024-06-07 7:01
* @author: lcs
*/
public class QRCodeUtil {
/**
* 创建二维码位图
*
* @param content 字符串内容(支持中文)
* @param width 位图宽度(单位:px)
* @param height 位图高度(单位:px)
* @return
*/
@Nullable
public static Bitmap createQRCodeBitmap(String content, int width, int height){
return createQRCodeBitmap(content, width, height, "UTF-8", "H", "2", Color.BLACK, Color.WHITE);
}
/**
* 创建二维码位图 (支持自定义配置和自定义样式)
*
* @param content 字符串内容
* @param width 位图宽度,要求>=0(单位:px)
* @param height 位图高度,要求>=0(单位:px)
* @param character_set 字符集/字符转码格式 (支持格式:{@link CharacterSetECI })。传null时,zxing源码默认使用 "ISO-8859-1"
* @param error_correction 容错级别 (支持级别:{@link ErrorCorrectionLevel })。传null时,zxing源码默认使用 "L"
* @param margin 空白边距 (可修改,要求:整型且>=0), 传null时,zxing源码默认使用"4"。
* @param color_black 黑色色块的自定义颜色值
* @param color_white 白色色块的自定义颜色值
* @return
*/
@Nullable
public static Bitmap createQRCodeBitmap(String content, int width, int height,
@Nullable String character_set, @Nullable String error_correction, @Nullable String margin,
@ColorInt int color_black, @ColorInt int color_white){
/** 1.参数合法性判断 */
if(TextUtils.isEmpty(content)){ // 字符串内容判空
return null;
}
if(width < 0 || height < 0){ // 宽和高都需要>=0
return null;
}
try {
/** 2.设置二维码相关配置,生成BitMatrix(位矩阵)对象 */
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
if(!TextUtils.isEmpty(character_set)) {
hints.put(EncodeHintType.CHARACTER_SET, character_set); // 字符转码格式设置
}
if(!TextUtils.isEmpty(error_correction)){
hints.put(EncodeHintType.ERROR_CORRECTION, error_correction); // 容错级别设置
}
if(!TextUtils.isEmpty(margin)){
hints.put(EncodeHintType.MARGIN, margin); // 空白边距设置
}
BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
/** 3.创建像素数组,并根据BitMatrix(位矩阵)对象为数组元素赋颜色值 */
int[] pixels = new int[width * height];
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
if(bitMatrix.get(x, y)){
pixels[y * width + x] = color_black; // 黑色色块像素设置
} else {
pixels[y * width + x] = color_white; // 白色色块像素设置
}
}
}
/** 4.创建Bitmap对象,根据像素数组设置Bitmap每个像素点的颜色值,之后返回Bitmap对象 */
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
}
绑定点击事件,点击跳转到身份牌列表
tvIdentity.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//实名认证
Intent intent = new Intent(bottomNavigationViewActivity.this, IdentityCardActivity.class);
startActivity(intent);
}
});
侧拉框的布局
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/background_v_3"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical"> <FrameLayout android:id="@+id/frame_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/bottom_navigation" /> <View android:layout_width="match_parent" android:layout_height="0.3dp" android:layout_above="@id/bottom_navigation" android:background="@color/black"/> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/background_bottom" app:menu="@menu/menu" tools:layout_height="50dp" /> </RelativeLayout> </LinearLayout> <androidx.drawerlayout.widget.DrawerLayout android:id="@+id/drawer_layout2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/linear_top" android:layout_width="match_parent" android:layout_height="260dp" android:orientation="vertical" android:background="@mipmap/background_h_1"> <LinearLayout android:layout_width="match_parent" android:layout_height="80dp" android:layout_marginTop="175dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@drawable/transparent_rectangle" android:orientation="horizontal"> <LinearLayout android:id="@+id/personal_edit" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="55dp" android:layout_height="50dp" android:layout_gravity="center" android:layout_marginTop="5dp" android:src="@drawable/personal_edit"></ImageView> <TextView android:text="个人信息" android:textColor="@color/white" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:textStyle="bold" android:layout_weight="9" android:layout_height="wrap_content" android:gravity="start" android:textSize="16dp" /> </LinearLayout> <LinearLayout android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/personal_like"> <ImageView android:layout_marginTop="5dp" android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center" android:src="@drawable/personal_like"></ImageView> <TextView android:text="喜欢" android:textColor="@color/white" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:textStyle="bold" android:layout_weight="9" android:layout_height="wrap_content" android:gravity="start" android:textSize="16dp" /> </LinearLayout> <LinearLayout android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/personal_collect"> <ImageView android:layout_width="55dp" android:layout_height="50dp" android:layout_gravity="center" android:layout_marginTop="5dp" android:src="@drawable/personal_collect02"></ImageView> <TextView android:text="收藏" android:textColor="@color/white" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_weight="9" android:layout_height="wrap_content" android:textStyle="bold" android:gravity="start" android:textSize="16dp" /> </LinearLayout> <LinearLayout android:id="@+id/personal_follow" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="55dp" android:layout_height="50dp" android:layout_marginTop="5dp" android:layout_gravity="center" android:src="@drawable/personal_follow"></ImageView> <TextView android:text="关注" android:textColor="@color/white" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:textStyle="bold" android:layout_weight="9" android:layout_height="wrap_content" android:gravity="start" android:textSize="16dp" /> </LinearLayout> </LinearLayout> </LinearLayout> <androidx.cardview.widget.CardView android:id="@+id/iv_info_cardview" android:layout_width="90dp" android:layout_height="90dp" android:layout_marginLeft="40dp" android:layout_marginTop="60dp" app:cardCornerRadius="100sp" android:elevation="20dp" android:translationZ="20dp" android:background="@drawable/avatar_circle_kuang"> <androidx.cardview.widget.CardView android:id="@+id/iv_mine_cardview" android:layout_width="80dp" android:layout_height="80dp" android:layout_centerHorizontal="true" android:elevation="20dp" android:translationZ="20dp" android:layout_gravity="center" app:cardCornerRadius="100sp"> <ImageView android:id="@+id/personal_avatar" android:layout_width="match_parent" android:layout_height="match_parent" android:elevation="20dp" android:translationZ="20dp" android:layout_centerHorizontal="true" android:scaleType="centerCrop" /> <ProgressBar android:id="@+id/progress_bar" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center" android:visibility="visible" /> </androidx.cardview.widget.CardView> </androidx.cardview.widget.CardView> <TextView android:id="@+id/personal_nick" android:layout_marginTop="70dp" android:layout_marginLeft="20dp" android:elevation="20dp" android:translationZ="20dp" android:layout_toRightOf="@+id/iv_info_cardview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="昵称" android:textStyle="bold" android:textSize="20dp" android:textColorHighlight="@color/black"/> <TextView android:id="@+id/personal_signature" android:layout_below="@+id/personal_nick" android:layout_toRightOf="@+id/iv_info_cardview" android:layout_marginLeft="20dp" android:elevation="20dp" android:translationZ="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:text="个性签名" android:textSize="16dp" /> <LinearLayout android:id="@+id/linear_bottom" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/linear_top" android:layout_marginTop="-2dp" android:background="@mipmap/background_many_cat03" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="20dp" android:background="@color/gray"> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_gravity="center" android:background="@android:color/darker_gray" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:layout_marginBottom="15dp" android:orientation="horizontal"> <TextView android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_marginLeft="10dp" android:textSize="16dp" android:text="收货地址" /> <TextView android:layout_weight="6" android:layout_width="wrap_content" android:id="@+id/personal_address" android:layout_height="20dp" android:textSize="16dp" android:text="未设置收货地址" android:textColor="@color/black" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_gravity="center" android:background="@android:color/darker_gray" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:layout_marginBottom="15dp" android:orientation="horizontal"> <TextView android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_marginLeft="10dp" android:textSize="16dp" android:text="手机号" /> <TextView android:layout_weight="6" android:id="@+id/personal_phone" android:layout_width="wrap_content" android:layout_height="20dp" android:textSize="16dp" android:textColor="@color/black"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_gravity="center" android:background="@android:color/darker_gray" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:layout_marginBottom="15dp" android:orientation="horizontal"> <TextView android:id="@+id/personal_pet_id" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_marginLeft="10dp" android:textSize="16dp" android:text="宠物身份牌" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_gravity="center" android:background="@android:color/darker_gray" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:id="@+id/personal_order" android:text="订单" android:textSize="16dp" android:layout_marginTop="10dp" android:layout_marginLeft="10dp"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_gravity="center" android:background="@android:color/darker_gray" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:layout_marginBottom="15dp" android:orientation="horizontal"> <TextView android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="20dp" android:layout_marginLeft="10dp" android:textSize="16dp" android:text="个人宠物" /> <TextView android:layout_weight="6" android:id="@+id/personal_pet" android:layout_width="wrap_content" android:layout_height="20dp" android:textSize="16dp" android:text="点此添加个人宠物" android:textColor="@color/black"/> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_gravity="center" android:background="@android:color/darker_gray" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_weight="1" android:id="@+id/personal_finish1" android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:textColor="@color/black" android:text="退出应用" /> <View android:layout_width="3dp" android:layout_height="match_parent" android:layout_gravity="center" android:background="@android:color/darker_gray" /> <TextView android:layout_weight="1" android:id="@+id/personal_exchange1" android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:textColor="@color/black" android:text="切换账号" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_gravity="center" android:background="@android:color/darker_gray" /> </LinearLayout> </RelativeLayout> </androidx.drawerlayout.widget.DrawerLayout> </androidx.drawerlayout.widget.DrawerLayout>
activity
package com.catanddog.Personal.activity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.catanddog.Personal.adapter.IdentityAdapter;
import com.catanddog.Personal.entity.Item;
import com.catanddog.R;
import java.util.ArrayList;
import java.util.List;
public class IdentityCardActivity extends AppCompatActivity {
private ImageView imageView;
private ListView listView;
@Override
@SuppressLint("MissingInflatedId")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_identity_card);
// 初始化控件
initViews();
// 给listView设置数据
List<Item> dataList = new ArrayList<>();
dataList.add(new Item("Title 1", R.drawable.default_avatar));
dataList.add(new Item("Title 2", R.drawable.default_avatar));
IdentityAdapter myAdapter = new IdentityAdapter(dataList);
listView.setAdapter(myAdapter);
// 设置点击事件
listView.setOnItemClickListener((parent, view, position, id) -> {
// 处理点击事件
Item item = dataList.get(position);
System.out.println("点击了第" + (position + 1) + "个item,标题是:" + item.getTitle());
// 跳转到生成的二维码页
Intent intent = new Intent(this, QRCodeActivity.class);
intent.putExtra("name", "小米");
intent.putExtra("sex", "男");
intent.putExtra("age", "8");
intent.putExtra("address", "北京");
intent.putExtra("phone", "173349876641");
intent.putExtra("email", "[email protected]");
intent.putExtra("idcard", "123456789");
intent.putExtra("image", "http://10.6.207.113:8080/catanddog/upload/lcs1.jpg");
startActivity(intent);
});
}
private void initViews() {
// imageView = findViewById(R.id.imageView_ercode);
listView = findViewById(R.id.listView_identitycard);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Personal.activity.IdentityCardActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView_identitycard" />
</LinearLayout>
adapter
package com.catanddog.Personal.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.catanddog.Personal.entity.Item;
import com.catanddog.R;
import java.util.List;
public class IdentityAdapter extends BaseAdapter {
private List<Item> mData; // 数据源
public IdentityAdapter(List<Item> data) {
mData = data;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.identity_item_layout, parent, false);
}
ImageView imageView = convertView.findViewById(R.id.image_view);
imageView.setImageResource(mData.get(position).getImageResource());
TextView textView = convertView.findViewById(R.id.text_view);
textView.setText(mData.get(position).getTitle());
return convertView;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/default_avatar"/>
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"/>
</LinearLayout>
生成二维码的activity
package com.catanddog.Personal.activity;
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.catanddog.R;
import com.catanddog.utils.QRCodeUtil;
public class QRCodeActivity extends AppCompatActivity {
private ImageView qrcode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qrcode);
// 初始化控件
initViews();
// 接受输入的二维码信息
String name = getIntent().getStringExtra("name");
String sex = getIntent().getStringExtra("sex");
String address = getIntent().getStringExtra("address");
String age = getIntent().getStringExtra("age");
String phone = getIntent().getStringExtra("phone");
String email = getIntent().getStringExtra("email");
String idcard = getIntent().getStringExtra("idcard");
String image = getIntent().getStringExtra("image");
// 拼接二维码信息,拼接后格式加上换行符
String qrcodeInfo = name + "\n" + sex + "\n" + address + "\n" + age + "\n" +phone ;
// 生成二维码
qrcode.setImageBitmap(QRCodeUtil.createQRCodeBitmap(qrcodeInfo, 200, 200));
}
private void initViews() {
qrcode = (ImageView) findViewById(R.id.qrcode_image);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Personal.activity.QRCodeActivity">
<ImageView
android:id="@+id/qrcode_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
</LinearLayout>
标签:String,int,width,二维码,实例,import,android,com From: https://blog.csdn.net/LIUCHANGSHUO/article/details/139623814