Android 二维码快速生成代码
2014-11-25 17:17
357 查看
<pre name="code" class="java">看了很多人在找关于二维码生成的代码 今天就抽时间写了一下,希望对大家有所帮助!
<a target=_blank href="http://download.csdn.net/download/u012974916/8197269" target="_blank">点击打开链接</a>
public class MainActivity extends Activity { private static final String TAG=MainActivity.class.getSimpleName(); private TextView qr_text; private ImageView qr_image; private final int QR_WIDTH=140; private final int QR_HEIGHT=140; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); qr_text=(TextView) findViewById(R.id.textView1); qr_image=(ImageView) findViewById(R.id.imageView1); createImage(); } // 生成QR图 private void createImage() { try { // 需要引入core包 QRCodeWriter writer = new QRCodeWriter(); String text = qr_text.getText().toString(); text="黄明明"; Log.i(TAG, "生成的文本:" + text); if (text == null || "".equals(text) || text.length() < 1) { return; } // 把输入的文本转为二维码 BitMatrix martix = writer.encode(text, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT); System.out.println("w:" + martix.getWidth() + "h:" + martix.getHeight()); Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints); int[] pixels = new int[QR_WIDTH * QR_HEIGHT]; for (int y = 0; y < QR_HEIGHT; y++) { for (int x = 0; x < QR_WIDTH; x++) { if (bitMatrix.get(x, y)) { pixels[y * QR_WIDTH + x] = 0xff000000; } else { pixels[y * QR_WIDTH + x] = 0xffffffff; } } } Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT); qr_image.setImageBitmap(bitmap); } catch (WriterException e) { e.printStackTrace(); } } } 布局文件记得自己写哦哦!!!http://download.csdn.net/download/u012974916/8197269
相关文章推荐
- Android控件绑定代码快速生成工具
- Android中字符串生成二维码只需简单的两句代码
- Java快速生成二维码(内附代码)
- Android快速实现二维码解析及生成模块(Zxing框架)
- android端快速生成二维码和进行二维码扫描
- android两行代码生成二维码示例
- Android 点击生成二维码功能实现代码
- Android二维码解析和生成(菜鸟的福利来了,70多行代码实现)
- Android二维码的生成与扫码-zxing示例代码
- android mvp快速开发框架介绍(自动生成android代码工具介绍)
- ORM Querier 基于Transact-SQL解析的代码生成利器 帮助开发人员高效快速生成需要的ORM代码
- android 如何用代码生成圆角Bitmap图片
- 二维码生成与解析代码实现
- 快速代码生成+基础框架
- silverlight与CSLA的快速应用07-利用CslaExtension自动生成CSLA代码
- 这几年的技术尝试,都有哪些失败的例子- 快速代码生成+基础框架
- 生成动态库so的方法|解决NDK r5b引用静态库失效的问题|在NDK的Nactive代码中使用Android Log的方法|生成Native java头文件
- Android2.2源代码中关于生成图片缩略图的一个bug
- Android 代码动态生成ProgressBar
- OPhone/Android的学习(2)—从分析Eclipse自动生成的代码到以XML控制UI