android 二维码制作,显示到UI,并保存SD卡,拿来就能用!!

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

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

包结构:

界面截图:

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

MainActivity(重要的部分已详细标注,生成的图片也经过测试可用)

{ImageView imageview;EditText webInput;Button MakeImage;Bitmap qrImage;private ProgressDialog mLoadingDialog;(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();}(){MakeImage.setOnClickListener(new OnClickListener() {(View v) {//判断是否有输入内容if(webInput.getText().toString().equals(“”)||webInput==null){Toast.makeText(MainActivity.this, “请输入网址”, Toast.LENGTH_SHORT).show();return;}else{showLoadingDialog(“Loading…”);//回收bitmapif(null != qrImage && !qrImage.isRecycled()){qrImage.recycle();qrImage = null;}try {qrImage = makeQRImage(webInput.getText().toString(), 600, 600);} catch (WriterException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();}dismissLoadingDialog();Toast.makeText(MainActivity.this, “保存成功”, Toast.LENGTH_LONG).show();}else{Toast.makeText(MainActivity.this, “没有安装SD卡”, Toast.LENGTH_LONG).show();}}}});}(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}(MenuItem item) {id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}(String msg) {if (mLoadingDialog != null && mLoadingDialog.isShowing()) {return;}mLoadingDialog = new ProgressDialog(this);mLoadingDialog.setMessage(msg);// mLoadingDialog.setOnKeyListener(mOnKeyListener);// mLoadingDialog.setCancelable(false);mLoadingDialog.show();}/*** 取消加载对话框*/() {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;elsepixels[y * width + x] = 0xffffffff;}}// 生成二维码图片的格式,使用ARGB_8888Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, width, 0, 0, width, height);return bitmap;}/*** 判断字符串是否非空非null* @param strParm 需要判断的字符串* @return 真假*/(String strParm){return ! ( (strParm == null) || (strParm.equals(“”)));}/*** 指定目录写入文件内容* @param filePath 文件路径+文件名* @param content 文件内容* @throws IOException*/(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();}}}() {if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {return true;} else {return false;}}}好好的管教你自己,不要管别人。

android 二维码制作,显示到UI,并保存SD卡,拿来就能用!!

相关文章:

你感兴趣的文章:

标签云: