利用开源ZXing库,在android上进行二维码简单的编码和解码
2014-04-09 19:09
531 查看
首先是简单的编码,代码如下
再次,是简单的解码,代码如下
点击此处下载示例源码
/** * 根据字符串进行二维编码 * @param str 需要编码的字符串 * @param widthAndHeight 需要生成的bitmap的高宽 * @return */ public Bitmap enCode(String str,int widthAndHeight){ if (!str.equals("")&&str!=null) try { return EncodingHandler.createQRCode(str, widthAndHeight); } catch (WriterException e) { // TODO Auto-generated catch block e.printStackTrace(); } else try { return EncodingHandler.createQRCode("null", widthAndHeight); } catch (WriterException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
再次,是简单的解码,代码如下
public String deCode(Bitmap bitmap) { if(bitmap ==null){ Log.i("deCode","-----------------------null"); return "null"; } Hashtable<DecodeHintType, Object> hints = null; initHints(hints, null, "UTF8"); MultiFormatReader multiFormatReader = new MultiFormatReader(); multiFormatReader.setHints(hints); LuminanceSource source = new BitmapLuance(bitmap); BinaryBitmap bit = new BinaryBitmap(new HybridBinarizer(source)); try { return multiFormatReader.decodeWithState(bit).getText(); } catch (ReaderException re) { // continue } finally { multiFormatReader.reset(); } return null; } public void initHints(Hashtable<DecodeHintType, Object> hints, Vector<BarcodeFormat> decodeFormats, String CODE_STYLE) { hints = new Hashtable<DecodeHintType, Object>(2); if (decodeFormats == null || decodeFormats.isEmpty()) { decodeFormats = new Vector<BarcodeFormat>(); decodeFormats.addAll(MyDecodeFormatManager.ONE_D_FORMATS); decodeFormats.addAll(MyDecodeFormatManager.QR_CODE_FORMATS); decodeFormats.addAll(MyDecodeFormatManager.DATA_MATRIX_FORMATS); } hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats); if (CODE_STYLE != null) { //hints.put(DecodeHintType.CHARACTER_SET, CODE_STYLE); } } private Bitmap fileToBitmap(String imgpath) { Bitmap bm = BitmapFactory.decodeFile(imgpath); return bm; }
点击此处下载示例源码
相关文章推荐
- Android利用mediacodec进行视频H264编码解码播放
- 【Java】利用Java对网址进行二维码的编码与解码操作
- Android利用mediacodec进行视频H264编码解码播放
- Android利用mediacodec进行视频H264编码解码播放
- Android利用mediacodec进行视频H264编码解码播放
- 利用OpenSSL进行BASE64的编码与解码
- [置顶] Android Multimedia框架总结(二十六)利用FFmpeg进行解码直播流
- GitHub 上排名前 100 的 Android 开源库进行简单的介绍
- Java利用QRCode.jar包实现二维码编码与解码
- Java利用QRCode.jar包实现二维码编码与解码
- Android简单的利用MediaRecorder进行录音
- 利用base64对图片进行编码及解码
- 【Android QR Code】开源项目:ZXing(三)二维码解码
- [Android开源]一个非常简单易用用来花式展示二维码样式生成的库QRCodeStyle
- Android开发之利用ZXing库实现二维码的生成
- 我的Android进阶之旅】GitHub 上排名前 100 的 Android 开源库进行简单的介绍
- 实用命令:利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密
- Android使用json对中文进行编码 使用php接收时进行解码
- Android java层与jni之间字符串数据传递需进行编码解码操作