首先导入jar包,下面是jar包的下载地址
http://files.cnblogs.com/files/lee0oo0/%E4%BA%8C%E7%BB%B4%E7%A0%81Jar%E5%8C%85.rar
生成二维码的工具类
public class QRCodeUtils {
private static final String TAG = "TAG";
/**
*
* @param context 上下文
* @param qr_text 输入框,拿到里面的内容
* @param qr_image ImagView 显示图片
* @return 返回Bitmap用于保存到手机sss图库
*/
public Bitmap createImage(Context context, EditText qr_text, ImageView qr_image) {
try {
// 需要引入core包
QRCodeWriter writer = new QRCodeWriter();
String text = qr_text.getText().toString();
Log.i(TAG, "生成的文本:" + text);
if (text == null || "".equals(text) || text.length() < 1) {
Toast.makeText(context, "请输入文字哦!", Toast.LENGTH_SHORT).show();
return null;
}
//参数1 文本 参数2 生成的类型为QR_CODE 二维码 参数3 宽 删除4 高
BitMatrix martix = writer.encode(text, BarcodeFormat.QR_CODE,
500, 500);
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
//设置文本类型与编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//绘制500*500的图
BitMatrix bitMatrix = new QRCodeWriter().encode(text,
BarcodeFormat.QR_CODE, 500, 500, hints);
int[] pixels = new int[500 * 500];
for (int y = 0; y < 500; y++) {
for (int x = 0; x < 500; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * 500 + x] = 0xff000000;
} else {
pixels[y * 500 + x] = 0xffffffff;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(500, 500,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, 500, 0, 0, 500, 500);
qr_image.setImageBitmap(bitmap);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
}布局文件
<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:orientation="vertical" tools:context="comiptv.example.vincent.paytest.MainActivity"> <ScrollView android:scrollbars="none" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="分享到朋友圈" android:textSize="30sp" android:visibility="gone"/> <Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="分享到好友会话" android:textSize="30sp" android:visibility="gone"/> <EditText android:id="@+id/tv1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="ByLiLiu"/> <Button android:id="@+id/bt" android:textSize="25sp" android:gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按我生成"/> <ImageView android:gravity="center_horizontal" android:id="@+id/iv1" android:layout_width="300dp" android:layout_height="300dp"/> <Button android:id="@+id/bt_save" android:textSize="25sp" android:gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按我保存"/> </LinearLayout> </ScrollView> </LinearLayout>
在MainActivity中调用
final EditText tv1 = (EditText) findViewById(R.id.tv1);
final ImageView iv1 = (ImageView) findViewById(R.id.iv1);
Button bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
QRCodeUtils qrCodeUtils = new QRCodeUtils();
image = qrCodeUtils.createImage(MainActivity.this, tv1, iv1);
}
});
findViewById(R.id.bt_save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null!=image){
saveMyBitmap(image);
}else {
Toast.makeText(MainActivity.this, "请输入文字!", Toast.LENGTH_SHORT).show();
}
}
});
}
private void saveMyBitmap(Bitmap image) {
ContentResolver cr = getContentResolver();
//拿到当前时间设置为文件名
String name=System.currentTimeMillis()+"";
//参数1 ContentResolver 参数2 ImagView 参数3 文件名 参数4 描述
MediaStore.Images.Media.insertImage(cr, image, name, "来自LiLiu");
Toast.makeText(this, "保存成功!", Toast.LENGTH_SHORT).show();
}看效果

随时随地看视频