package request.http.com.httprequest.fragment.my;
import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
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.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.nereo.multi_image_selector.MultiImageSelector;
import request.http.com.httprequest.R;
import request.http.com.httprequest.activity.MainActivity;
import request.http.com.httprequest.bean.avatarBean;
import request.http.com.httprequest.common.Tools;
import request.http.com.httprequest.fragment.main.homeMyFragment;
import static android.app.Activity.RESULT_OK;
import static android.content.Context.MODE_PRIVATE;
import static android.os.Environment.getExternalStorageDirectory;
/**
* Date: 2019/7/15
* Time: 17:03
* author:Guest
*/
public class avatarFragment extends Fragment {
@BindView(R.id.iv_qrcode)
ImageView iv_qrcode;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_avatar , container , false);
Unbinder unbinder = ButterKnife.bind(this,view);
Message message = new Message();
message.arg1 = 202;
MainActivity.mainHandler.sendMessage(message);
initView();
initData();
return view;
}
private void initView(){
}
private void initData(){
QRCode("https://www.baidu.com/",1900,1900);
}
/**
* 生成带有图片的二维码
* @param content
* @param width
* @param height
*/
private void QRCode(String content, int width, int height) {
try {
//1,创建实例化对象
QRCodeWriter writer = new QRCodeWriter() ;
//2,设置字符集
Map<EncodeHintType, String> map = new HashMap<EncodeHintType, String>();
map.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//3,通过encode方法将内容写入矩阵对象
BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height,map);
//4,定义一个二维码像素点的数组,向每个像素点中填充颜色
int[] pixels = new int[width*height];
//5,往每一像素点中填充颜色(像素没数据则用黑色填充,没有则用彩色填充,不过一般用白色)
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (matrix.get(j, i)) {
pixels[i*width+j] = 0xff000000;
}else {
pixels[i*width+j] = 0xffffffff;
}
}
}
//6,创建一个指定高度和宽度的空白bitmap对象
Bitmap bm_QR = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
//7,将每个像素的颜色填充到bitmap对象
bm_QR.setPixels(pixels, 0, width, 0, 0, width, height);
//8,创建一个bitmap对象用于作为其图标
Bitmap bm_login = BitmapFactory.decodeResource(getResources(), R.mipmap.chat_friends_person8);
//9,创建一个方法在二维码上添加一张图片
if (addLogin(bm_QR,bm_login) != null) {
iv_qrcode.setImageBitmap(addLogin(bm_QR,bm_login));
}
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 用于向创建的二维码中添加一个logn
* @param bm_QR
* @param bm_login
* @return
*/
private Bitmap addLogin(Bitmap bm_QR, Bitmap bm_login) {
if (bm_QR == null) {
return null;
}
if (bm_login == null) {
return bm_QR ;
}
//获取图片的宽高
int bm_QR_Width = bm_QR.getWidth() ;
int bm_QR_Height = bm_QR.getHeight();
int bm_login_Width = bm_login.getWidth() ;
int bm_login_Height = bm_login.getHeight();
//设置logn的大小为二维码整体大小的1/5
float scale_login = bm_QR_Width*1.0f /5/bm_login_Width ;
Bitmap bitmap = Bitmap.createBitmap(bm_QR_Width, bm_QR_Height, Bitmap.Config.ARGB_8888);
try {
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bm_QR, 0, 0, null);
canvas.scale(scale_login, scale_login, bm_QR_Width / 2, bm_QR_Height / 2);
canvas.drawBitmap(bm_login, (bm_QR_Width - bm_login_Width) / 2, (bm_QR_Height - bm_login_Height) / 2, null);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
} catch (Exception e) {
bitmap = null;
e.getStackTrace();
}
return bitmap;
}
/**
* 生成没有图片的二维码
* @param content: 地址,生成二维码地址
* @param width:二维码的宽度
* @param height:二维码的高度
*/
private void QRCode(String content, int width, int height) {
try {
//1,创建实例化对象
QRCodeWriter writer = new QRCodeWriter() ;
//2,设置字符集
Map<EncodeHintType, String> map = new HashMap<EncodeHintType, String>();
map.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//3,通过encode方法将内容写入矩阵对象
BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height,map);
//4,定义一个二维码像素点的数组,向每个像素点中填充颜色
int[] pixels = new int[width*height];
//5,往每一像素点中填充颜色(像素没数据则用黑色填充,没有则用彩色填充,不过一般用白色)
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (matrix.get(j, i)) {
pixels[i*width+j] = 0xff000000;
}else {
pixels[i*width+j] = 0xffffffff;
}
}
}
//6,创建一个指定高度和宽度的空白bitmap对象
Bitmap bm_QR = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
//7,将每个像素的颜色填充到bitmap对象
bm_QR.setPixels(pixels, 0, width, 0, 0, width, height);
iv_qrcode.setImageBitmap(bm_QR);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
android Zxing生成带有图片二维码
最新推荐文章于 2023-06-22 14:19:23 发布