抽出一点时间整理下,因为自己平时喜欢睡懒觉,嘿嘿!二维码非常普遍了,看到新闻上有老奶奶乞讨都用二维码了,到现在要是再不会用,是不是有点落伍了!
1.准备工作,下载Zxing的jar包
点击下载
下载好后,新建项目后拷贝进我们的libs文件夹下—》Add AS library —》OK!
上代码了———————————————————–
布局文件:
<?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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.btzh.myjavatest.MainActivity">
<EditText
android:id="@+id/main_edit"
android:textSize="18sp"
android:hint="请输入..."
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:layout_below="@id/main_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="输入框二维码"
android:id="@+id/main_click_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="图片二维码"
android:id="@+id/main_picture_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="选择图片二维码"
android:id="@+id/main_choose_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:id="@+id/main_image_view"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_alignParentBottom="true"
android:id="@+id/main_introduce_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Activity代码:
package com.btzh.myjavatest;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
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.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button edit_btn;
private Button pic_btn;
private Button chos_btn;
private TextView intro_text;
private ImageView view;
private EditText editText;
private String Strhttp = "http://b287.photo.store.qq.com/psb?/" +
"afb326ef-e39d-4536-a898-b867963b6631/w6k.RR0ibYz72u64E7jGiZ5uGjf151r3lBE1BgBn*bk!/" +
"c/dB8BAAAAAAAA&bo=VQOAAgAFwAMFCDk!";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
public void initViews(){
edit_btn = (Button)findViewById(R.id.main_click_btn);
edit_btn.setOnClickListener(this);
pic_btn = (Button)findViewById(R.id.main_picture_btn);
pic_btn.setOnClickListener(this);
chos_btn = (Button)findViewById(R.id.main_choose_btn);
chos_btn.setOnClickListener(this);
editText = (EditText)findViewById(R.id.main_edit);
view = (ImageView)findViewById(R.id.main_image_view);
intro_text = (TextView)findViewById(R.id.main_introduce_text);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.main_click_btn:
intro_text.setText("");
view.setImageDrawable(null);
//根据输入内容生成二维码
Bitmap qrBitmap = generateBitmap(editText.getText().toString(),400, 400);
if (null != qrBitmap){
view.setImageBitmap(qrBitmap);
intro_text.setText("我是输入内容二维码!");
}
break;
case R.id.main_picture_btn:
intro_text.setText("");
//清空输入框
editText.setText("");
view.setImageDrawable(null);
Bitmap maphttp = generateBitmap(Strhttp,500,500);
//把要添加的中心logo图片转换成bitmap形式
Bitmap logoBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.wang);
//添加到二维码中心
Bitmap bitmap = addLogo(maphttp, logoBitmap);
if (null != bitmap){
//显示二维码
view.setImageBitmap(bitmap);
intro_text.setText("我是带有中心logo二维码!");
}
break;
case R.id.main_choose_btn:
intro_text.setText("");
editText.setText("");
//打开手机相册
default:
break;
}
}
//对输入的内容生成二维码
private Bitmap generateBitmap(String content, int width, int height) {
if("".equals(content)||null==content){
Toast.makeText(MainActivity.this,"输入内容为空!",Toast.LENGTH_SHORT).show();
return null;
}
QRCodeWriter qrCodeWriter = new QRCodeWriter();
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
int[] pixels = new int[width * height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (encode.get(j, i)) {
pixels[i * width + j] = 0x00000000;
} else {
pixels[i * width + j] = 0xffffffff;
}
}
}
return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
//添加二维码中心logo
private Bitmap addLogo(Bitmap qrBitmap, Bitmap logoBitmap) {
if (null==logoBitmap){
Toast.makeText(MainActivity.this,"所选图片为空!",Toast.LENGTH_SHORT).show();
return null;
}
int qrBitmapWidth = qrBitmap.getWidth();
int qrBitmapHeight = qrBitmap.getHeight();
int logoBitmapWidth = logoBitmap.getWidth();
int logoBitmapHeight = logoBitmap.getHeight();
Bitmap blankBitmap = Bitmap.createBitmap(qrBitmapWidth, qrBitmapHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(blankBitmap);
canvas.drawBitmap(qrBitmap, 0, 0, null);
canvas.save(Canvas.ALL_SAVE_FLAG);
float scaleSize = 1.0f;
while ((logoBitmapWidth / scaleSize) > (qrBitmapWidth / 5) || (logoBitmapHeight / scaleSize) > (qrBitmapHeight / 5)) {
scaleSize *= 2;
}
float sx = 1.0f / scaleSize;
canvas.scale(sx, sx, qrBitmapWidth / 2, qrBitmapHeight / 2);
canvas.drawBitmap(logoBitmap, (qrBitmapWidth - logoBitmapWidth) / 2, (qrBitmapHeight - logoBitmapHeight) / 2, null);
canvas.restore();
return blankBitmap;
}
@Override
protected void onDestroy() {
super.onDestroy();
//释放图片资源
view.setImageDrawable(null);
}
}
然后就搞定了,其中选择图片二维码会在下一个文章里写出来,注释很详细,相信就是安卓小白也能看懂吧!