• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
总有奸臣想害朕
博客园    首页    新随笔    联系   管理    订阅  订阅

二维码的生成

我们目前用的是谷歌的zxing来生成二维码;下面呢我分别为大家介绍一下简单的二维码,中间有log的二维码,和彩色二维码;

需要两个权限;

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.VIBRATE"/>

简单的二维码

首先你点击的按钮去扫一扫二维码

main.xml,这个页面可以自定义

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="Create2QR"
        android:text="生成二维码" />

    <EditText
        android:id="@+id/ednaem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入要生成的文字" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

  camera.xml;直接沾

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <SurfaceView
        android:id="@+id/preview_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <com.zxing.view.ViewfinderView
        android:id="@+id/viewfinder_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:background="#b0e0e6"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:text="开始扫描"
            android:textColor="@android:color/white"
            android:textSize="24sp"
            android:textStyle="bold" />
        
        
        <Button 
            android:id="@+id/openLight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="80dp"
            android:layout_centerHorizontal="true"
            android:text="开启闪光灯"
            android:textColor="@android:color/white"
            android:onClick="IfOpenLight"
            android:background="#00000000"
            android:textSize="16sp"/>
        
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:layout_below="@id/openLight"
            android:layout_centerHorizontal="true"
            android:text="从相册中选取照片"
            android:textColor="@android:color/white"
            android:onClick="pickPictureFromAblum"
            android:background="#00000000"
            android:textSize="16sp"/>

        <Button
            android:id="@+id/btn_cancel_scan"
            android:layout_width="200dp"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            android:layout_centerInParent="true"
            android:layout_marginBottom="75dp"
            android:background="#FFA500"
            android:text="取    消"
            android:textSize="24sp"
            android:textStyle="bold" />
    </RelativeLayout>

</FrameLayout>

  Mainactivity.java

package com.exaple.day09rikao;

import com.google.zxing.WriterException;
import com.zxing.zxingframe.BitmapUtil;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private ImageView image;
	private EditText edname;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		image = (ImageView) findViewById(R.id.image);
		edname = (EditText) findViewById(R.id.ednaem);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	// 鐢熸垚浜岀淮鐮�
	public void Create2QR(View v) {
		String uri = edname.getText().toString();
		// Bitmap bitmap = BitmapUtil.create2DCoderBitmap(uri, mScreenWidth/2,
		// mScreenWidth/2);
		Bitmap bitmap;
		try {
			bitmap = BitmapUtil.createQRCode(uri, 400);
			//鐢熸垚浜岀淮鐮乴og
			//bitmap = addLogo(bitmap, BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ke));

			if (bitmap != null) {
				image.setImageBitmap(bitmap);
			}

		} catch (WriterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	/**
     * 鍦ㄤ簩缁寸爜涓棿娣诲姞Logo鍥炬
     */
    private static Bitmap addLogo(Bitmap src, Bitmap logo) {
        if (src == null) {
            return null;
        }
 
        if (logo == null) {
            return src;
        }
 
        //鑾峰彇鍥剧墖鐨勫楂�
        int srcWidth = src.getWidth();
        int srcHeight = src.getHeight();
        int logoWidth = logo.getWidth();
        int logoHeight = logo.getHeight();
 
        if (srcWidth == 0 || srcHeight == 0) {
            return null;
        }
 
        if (logoWidth == 0 || logoHeight == 0) {
            return src;
        }
 
        //logo澶у皬涓轰簩缁寸爜鏁翠綋澶у皬鐨�/5
        float scaleFactor = srcWidth * 1.0f / 5 / logoWidth;
        Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
        try {
            Canvas canvas = new Canvas(bitmap);
            canvas.drawBitmap(src, 0, 0, null);
            canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
            canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);
 
            canvas.save(Canvas.ALL_SAVE_FLAG);
            canvas.restore();
        } catch (Exception e) {
            bitmap = null;
            e.getStackTrace();
        }
 
        return bitmap;
        
    }


}

  

posted @ 2016-04-19 09:52  蓝色的张杰  阅读(409)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3