Zxing小的demo

来源:互联网 发布:期货盈利交易系统优化 编辑:程序博客网 时间:2024/05/25 21:35

因为我用的是jar包是3.2.0的如果想要实现该demo需要下载一个jar.同时提供一个简单的封装类用来实现产生二维码.和保存,并附带一个activity.供验证者实现,也希望

大神多多指点


util:

public class ZxingUtil {    /**     * 生成二维码Bitmap     * @param content   内容     * @param width  图片宽度     * @param height 图片高度     * @param filePath  用于存储二维码图片的文件路径     * @return 生成二维码及保存文件是否成功     */    public static boolean createZxingImage(String content, int width, int height, String filePath) {        try {            if (content == null || "".equals(content)) {                return false;            }            //配置参数            Map<EncodeHintType, Object> hints = new HashMap<>();            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");            //容错级别            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);            // 图像数据转换,使用了矩阵转换            BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);            int[] pixels = new int[width * height];            // 下面这里按照二维码的算法,逐个生成二维码的图片,            // 两个for循环是图片横列扫描的结果            for (int y = 0; y < height; y++) {                for (int x = 0; x < width; x++) {                    if (bitMatrix.get(x, y)) {                        pixels[y * width + x] = 0xff000000;                    } else {                        pixels[y * width + x] = 0xffffffff;                    }                }            }            // 生成二维码图片的格式,使用ARGB_8888            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);            //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大!            return bitmap != null && bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath));        } catch (WriterException | IOException e) {            e.printStackTrace();        }        return false;    }}
activity:

public class MainActivity extends AppCompatActivity {    private EditText editText;    private ImageView imageView;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //内容        editText = (EditText) findViewById(R.id.create_qr_content);        //显示二维码图片        imageView = (ImageView) findViewById(R.id.create_qr_iv);        Button createQrBtn = (Button) findViewById(R.id.create_qr_btn);        createQrBtn.setOnClickListener(new View.OnClickListener(){            @Override            public void onClick (View v){                final String filePath = getFileRoot(MainActivity.this) + File.separator                        + "xym" + System.currentTimeMillis() + ".jpg";                //二维码图片较大时,生成图片、保存文件的时间可能较长,因此放在新线程中                new Thread(new Runnable() {                    @Override                    public void run() {                        boolean success = ZxingUtil.createZxingImage(editText.getText().toString().trim(), 800, 800,                                filePath);                        if (success) {                            runOnUiThread(new Runnable() {                                @Override                                public void run() {                                    imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));                                }                            });                        }                    }                }).start();            }        });    }    //文件存储根目录    private String getFileRoot(Context context) {        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {            File external = context.getExternalFilesDir(null);            if (external != null) {                return external.getAbsolutePath();            }        }        return context.getFilesDir().getAbsolutePath();    }}

xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.jzbwlkj.zxing.MainActivity">    <EditText        android:layout_centerVertical="true"        android:id="@+id/create_qr_content"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="请输入内容"/>    <ImageView        android:id="@+id/create_qr_iv"        android:layout_centerInParent="true"        android:layout_width="100dp"        android:layout_height="100dp" />    <Button        android:id="@+id/create_qr_btn"        android:layout_centerHorizontal="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="产生"/></RelativeLayout>



0 0
原创粉丝点击