转载请注明出处:王亟亟的大牛之路

如今二维码已经渗透了我们的生活。各种扫码关注啊。扫码下载的,今天上一个依据输入内容生成二维码的功能。

包结构:



界面截图:

功能:输入网址–>生成图片–>显示到Imageview–>储存到本地SD卡中

MainActivity(重要的部分已具体标注,生成的图片也经过測试可用)

public class MainActivity extends Activity {
ImageView imageview;
EditText webInput;
Button MakeImage;
Bitmap qrImage;
private ProgressDialog mLoadingDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webInput=(EditText)findViewById(R.id.webInput);
imageview=(ImageView)findViewById(R.id.imageview);
MakeImage=(Button)findViewById(R.id.MakeImage);
//业务逻辑
doBusiness();
} public void doBusiness(){
MakeImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//推断是否有输入内容
if(webInput.getText().toString().equals("")||webInput==null){
Toast.makeText(MainActivity.this, "请输入网址", Toast.LENGTH_SHORT).show();
return;
}else{
showLoadingDialog("Loading...");
//回收bitmap
if(null != qrImage && !qrImage.isRecycled()){
qrImage.recycle();
qrImage = null;
}
try {
qrImage = makeQRImage(webInput.getText().toString(), 600, 600);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imageview.setImageBitmap(qrImage);
//生成图片
if(isMountedSDCard()){
String filePath =Environment.getExternalStorageDirectory()+ "/MyLive/QRImage/"+"aa"+".jpg";
try {
//保存图片
saveAsJPEG(qrImage, filePath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dismissLoadingDialog();
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this, "没有安装SD卡", Toast.LENGTH_LONG).show();
} }
}
}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/*显示对话框*/
public void showLoadingDialog(String msg) {
if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
return;
}
mLoadingDialog = new ProgressDialog(this);
mLoadingDialog.setMessage(msg);
// mLoadingDialog.setOnKeyListener(mOnKeyListener);
// mLoadingDialog.setCancelable(false);
mLoadingDialog.show();
} /**
* 取消载入对话框
*/
public void dismissLoadingDialog() {
if (mLoadingDialog != null) {
mLoadingDialog.dismiss();
}
} /**
* 依据指定内容生成自己定义宽高的二维码图片
* @param content 须要生成二维码的内容
* @param width 二维码宽度
* @param height 二维码高度
* @throws WriterException 生成二维码异常
*/
public static Bitmap makeQRImage(String content, int width, int height)
throws WriterException {
// 推断URL合法性
if (!isNoBlankAndNoNull(content))
return null; Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 图像数据转换,使用了矩阵转换
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); return bitmap;
} /**
* 推断字符串是否非空非null
* @param strParm 须要推断的字符串
* @return 真假
*/
public static boolean isNoBlankAndNoNull(String strParm)
{
return ! ( (strParm == null) || (strParm.equals("")));
} /**
* 指定文件夹写入文件内容
* @param filePath 文件路径+文件名称
* @param content 文件内容
* @throws IOException
*/
public static void saveAsJPEG(Bitmap bitmap,String filePath)
throws IOException {
FileOutputStream fos = null; try {
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100,fos);
fos.flush();
} finally {
if (fos != null) {
fos.close();
}
}
} public static boolean isMountedSDCard() {
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
return true;
} else {
return false;
}
}
}

布局文件:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.qrcodedemo.MainActivity" > <ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/webInput"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/webInput"
android:text="输入网址:"
android:textSize="16sp"
android:gravity="bottom"/> <EditText
android:id="@+id/webInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView1"
android:ems="10" >
<requestFocus />
</EditText> </LinearLayout> <Button
android:id="@+id/MakeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MakeImage" />
<ImageView
android:id="@+id/imageview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/> </LinearLayout>
</ScrollView>
</RelativeLayout>

源代码地址:http://yunpan.cn/cd8sknyybpZzE 訪问password 8f62

由于在代码中已经注明,所下面源代码看了用就可以。不做过多解释用到的JAR包也在源代码内

android 二维码制作,显示到UI,并保存SD卡,拿来就能用!!的更多相关文章

  1. android 二维码生成+扫描

    android 二维码生成+扫描 1.在Android应用当中,很多时候都要用到二维码扫描,来避免让用户手动输入的麻烦. Google官方自己推出了一个二维码开源项目:ZXing库. 2.这里简单介绍 ...

  2. Android二维码开源项目zxing用例简化和生成二维码、条形码

    上一篇讲到:Android二维码开源项目zxing编译,编译出来后有一个自带的測试程序:CaptureActivity比較复杂,我仅仅要是把一些不用的东西去掉,用看起来更方便,二维码和条形码的流行性自 ...

  3. Python 二维码制作

    Python 二维码制作 先介绍python 二维码制作的第三方库 QRCode .MyQR QRCode    生成这个二维码只用三行 import qrcode img = qrcode.make ...

  4. Android二维码识别 开源项目ZXing的编译

    Android二维码识别 开源项目ZXing的编译 Android端的条形码/二维码识别功能 因为手机端的输入不是很方便,所以条形码/二维码的扫描是一种很有效的解决手段. 比较流行的手机应用中,常用的 ...

  5. Android二维码扫描、生成

    Android二维码扫描.生成 现在使用二维码作为信息的载体已经越来越普及,那么二维码的生成以及扫描是如何实现的呢 google为我们提供了zxing开源库供我们使用 zxing GitHub源码地址 ...

  6. 二维码制作分享-Python

    分享一个简单快捷的二维码制作,Python实现. 1.安装准备 已安装的Python+Pycharm的计算机.本人win7+Python3.6+Pycharm 2.库包下载安装 Python二维码制作 ...

  7. Python 支付宝红包二维码制作步骤分享

    本文所有教程及源码.软件仅为技术研究.不涉及计算机信息系统功能的删除.修改.增加.干扰,更不会影响计算机信息系统的正常运行.不得将代码用于非法用途,如侵立删! 支付宝红包二维码制作步骤分享 2022. ...

  8. android 二维码扫描

    了解二维码这个东西还是从微信 中,当时微信推出二维码扫描功能,自己感觉挺新颖的,从一张图片中扫一下竟然能直接加好友,不可思议啊,那时候还不了解二维码,呵呵,然后做项目的时候, 老板说要加上二维码扫描功 ...

  9. Android二维码的生成,解析以及扫描功能

    <1> 布局只有2个按钮,实现生成二维码和解析二维码 <Button android:layout_width="wrap_content" android:la ...

随机推荐

  1. Debian下载地址

    http://cdimage.debian.org/cdimage/archive/

  2. threadlocal彻底理解

    如果你定义了一个单实例的java bean,它有若干属性,但是有一个属性不是线程安全的,比如说HashMap.并且碰巧你并不需要在不同的线程中共享这个属性,也就是说这个属性不存在跨线程的意义.那么你不 ...

  3. functor

    I thought it would be easy and convenient to define a small functor and perform a customized sort on ...

  4. jstat命令的使用及VM Thread分析

    http://www.javatang.com/archives/2017/10/20/12131956.html 前面提到了一个使用jstack的shell脚本,通过命令可以很快地定位到指定线程对应 ...

  5. cmd.exe启动参数说明

    启动命令解释程序 Cmd.exe 的新范例.如果在不含参数的情况下使用,cmd 将显示操作系统的版本和版权信息. 语法 cmd [{/c | /k}] [/s] [/q] [/d] [{/a | /u ...

  6. Ubuntu启动sshd服务

    1.Ubuntu主机安装ssh相关服务 openssh-client openssh-server 方法: sudo apt-get install openssh-client openssh-se ...

  7. 什么是ICE (Internet Communications Engine)

    http://user.qzone.qq.com/77811970 直接在google上搜索ICE,出来的结果并不多,所以很多人就认为ICE是个神秘的东西,其实,国内已经有很多大型应用在使用ICE了. ...

  8. PS 文字有锯齿怎么办

    1 可以在矢量绘图软件里面做,就没有锯齿了,画好之后导入到PS即可. 2 可以把PSD文件的像素值变大一些,比如调成500像素/英寸,但是这样会导致做出来的东西体积比较大,所以最好还是学会矢量绘图.

  9. 学习Opencv 2.4.9(二) ---操作像素

    作者:咕唧咕唧liukun321 来自:http://blog.csdn.net/liukun321 本质上说一张图像就是由数值组成的矩阵.Opencv 2.x由 cv::Mat 这个数据结构来表示一 ...

  10. js控制div内的滚动条的位置

    通过div的scrollTop变动控制垂直滚动条位置. 通过div的scrollLeft变动控制水平滚动条位置. 示例: <body> //d1是外层div,带滚动条 <div id ...