zoukankan      html  css  js  c++  java
  • android 二维码制作,显示到UI,并保存SD卡,拿来就能用!!

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

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

    包结构:
    这里写图片描写叙述
    界面截图:
    这里写图片描写叙述

    这里写图片描写叙述

    功能:输入网址–>生成图片–>显示到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包也在源代码内

  • 相关阅读:
    codeforces 587B
    codeforces 552E
    算法竞赛模板 字典树
    算法竞赛模板 二叉树
    算法竞赛模板 图形面积计算
    TZOJ 1545 Hurdles of 110m(动态规划)
    算法竞赛模板 判断线段相交
    算法竞赛模板 折线分割平面
    TZOJ 3005 Triangle(判断点是否在三角形内+卡精度)
    算法竞赛模板 KMP
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7234421.html
Copyright © 2011-2022 走看看