android 二维码制作,显示到UI,并保存SD卡,拿来就能用!!
2015-08-14 10:07
656 查看
转载请注明出处:王亟亟的大牛之路
现在二维码已经渗透了我们的生活,各种扫码关注啊,扫码下载的,今天上一个根据输入内容生成二维码的功能。包结构:
界面截图:
功能:输入网址–>生成图片–>显示到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 访问密码 8f62
因为在代码中已经注明,所以下源码看了用即可,不做过多解释用到的JAR包也在源码内
相关文章推荐
- hdu 5301 Buildings
- 创建型模式:建造者模式(Builder Pattern)
- Havok_2014-1-0_Pc_Xs_User_Guide(2.3.2-一般监听器)
- UITextField-设置大全-placeholder左上角显示
- hdu - 3836 Equivalent Sets(强连通)
- popupwindow实现炫酷动画UI
- SSIS 错误代码 DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER(0xC0202009)
- systemverilog中$value$plusargs的用法
- POJ 3080 Blue Jeans
- Windows Server 2016 build 10514系统截图曝光
- java.lang.IllegalArgumentException: Illegal character in query at index ...解决办法
- Value Stewardship Over Showmanship
- UINavigationBar的系统渲染方式
- 用Ionic框架+HBuilder开发的一些观察
- UIScrollView+UIPageControl
- 根据map的value值排序并输出
- h5+ mui笔记
- h5+ mui笔记
- String,StringBuffer,StringBuilder区别总结
- ueditor不自动加P解决方法