1. 程式人生 > >安卓將URL連結生成二維碼儲存到本地相簿

安卓將URL連結生成二維碼儲存到本地相簿

/*
*context不解釋;url:要轉二維碼的連結;width,height也不解釋;originalid:二維碼圖片的名字
*/
public static String createQRImage(Context context, String url, final int width, final int height, String oirginalid) {
    try {
        // 判斷URL合法性
        if (url == null || "".equals(url) || url.length() < 1) {
            return "fail";
        }
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 影象資料轉換,使用了矩陣轉換
        BitMatrix bitMatrix = new QRCodeWriter().encode(url, 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(pixels, 0, width, width, height, Bitmap.Config.RGB_565);

       /* String paths = "";
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File external = context.getExternalFilesDir(null);
            if (external != null) {
                paths = external.getAbsolutePath();
            }else{
                paths = context.getFilesDir().getAbsolutePath();
            }
        }else{
            paths = context.getFilesDir().getAbsolutePath();
        }*/
        /*String paths= Environment.getExternalStorageDirectory()
                + File.separator + Environment.DIRECTORY_DCIM
                +File.separator+"Camera"+File.separator;*/

        //File appDir = new File(paths);
        //File appDir = new File(Environment.getExternalStorageDirectory(), "Test");

        //File appDir = new File(Environment.getExternalStorageDirectory(), "DCIM");
        File appDir = new File(Environment.getExternalStorageDirectory(), "Pictures");
        if (!appDir.exists()) {
            appDir.mkdirs();
        }
        String fileName = oirginalid + ".png";
        android.util.Log.i("maptrix", "@#$%^&*------------------------bindphone message----------------------------"+fileName+"---"+appDir.getAbsolutePath());
        File file = new File(appDir, fileName);
        try {
            if(!file.exists()){
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            bitmap.recycle();
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "fail";
        } catch (IOException e) {
            e.printStackTrace();
            return "fail";
        }
        bitmap.recycle();
        // 其次把檔案插入到系統圖庫
        String path = file.getAbsolutePath();
        filePath = path;
        /*try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(), path, fileName, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "fail";
        }*/
        // 最後通知相簿更新
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(file);
        intent.setData(uri);
        context.sendBroadcast(intent);
        return path;
    } catch (WriterException e) {
        e.printStackTrace();
        return "fail";
    }
}

注意:第一,關於檔案讀寫,對應用許可權做好處理。第二,有個重新整理相簿那步我註釋掉了,不需要,否則會出現又重新搞了一張二維碼出來。