Android生成带图片的二维码
2015-05-15 16:27
423 查看
一、问题描述 |
二、案例介绍 |
1、案例运行效果
public class MainActivity extends Activity { private EditText etCompany; private EditText etPhone; private EditText etEmail; private EditText etWeb; private Bitmap logo; private static final int IMAGE_HALFWIDTH = 40;//宽度值,影响中间图片大小 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获得资源图片,可改成获取本地图片或拍照获取图片 logo=BitmapFactory.decodeResource(super.getResources(),R.drawable.y014); etCompany =(EditText) findViewById(R.id.etCompany); etPhone=(EditText) findViewById(R.id.etPhone); etEmail =(EditText) findViewById(R.id.etEmail); etWeb =(EditText) findViewById(R.id.etWeb); findViewById(R.id.but).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String company=etCompany.getText().toString().trim() ; String phone =etPhone .getText().toString().trim() ; String email = etEmail.getText().toString().trim() ; String web = etWeb.getText().toString().trim() ; //二维码中包含的文本信息 String contents= "BEGIN:VCARD\nVERSION:3.0\nORG:"+company+"\nTEL:"+phone+"\nURL:"+web+"\nEMAIL:"+email+"\nEND:VCARD"; try { //调用方法createCode生成二维码 Bitmap bm=createCode(contents,logo,BarcodeFormat.QR_CODE); ImageView img=(ImageView)findViewById(R.id.imgCode) ; //将二维码在界面中显示 img.setImageBitmap(bm); } catch (WriterException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } /** * 生成二维码 * @param string 二维码中包含的文本信息 * @param mBitmap logo图片 * @param format 编码格式 * @return Bitmap 位图 * @throws WriterException */ public Bitmap createCode(String string,Bitmap mBitmap, BarcodeFormat format) throws WriterException { Matrix m = new Matrix(); float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth(); float sy = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getHeight(); m.setScale(sx, sy);//设置缩放信息 //将logo图片按martix设置的信息缩放 mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), m, false); MultiFormatWriter writer = new MultiFormatWriter(); Hashtable<EncodeHintType, String> hst = new Hashtable<EncodeHintType, String>(); hst.put(EncodeHintType.CHARACTER_SET, "UTF-8");//设置字符编码 BitMatrix matrix = writer.encode(string, format, 400, 400, hst);//生成二维码矩阵信息 int width = matrix.getWidth();//矩阵高度 int height = matrix.getHeight();//矩阵宽度 int halfW = width / 2; int halfH = height / 2; int[] pixels = new int[width * height];//定义数组长度为矩阵高度*矩阵宽度,用于记录矩阵中像素信息 for (int y = 0; y < height; y++) {//从行开始迭代矩阵 for (int x = 0; x < width; x++) {//迭代列 if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH && y > halfH - IMAGE_HALFWIDTH && y < halfH + IMAGE_HALFWIDTH) {//该位置用于存放图片信息 //记录图片每个像素信息 pixels[y * width + x] = mBitmap.getPixel(x - halfW + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH); } else { if (matrix.get(x, y)) {//如果有黑块点,记录信息 pixels[y * width + x] = 0xff000000;//记录黑块信息 } } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // 通过像素数组生成bitmap bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } }
View Code
另,如果需要将将产生的二维码保存到磁盘中,可参考博文/article/5043150.html中BitmapUtil的saveBmpToSd()方法
想要进一步了解的同学,可以点击查看源代码,亲自运行体验!
作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/
本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
相关文章推荐
- android二维码图片生成模糊的问题
- Android平台利用Zxing生成二维码与解析图片中的二维码
- Android 生成带图片的二维码
- Android生成带LOGO图片二维码的方法
- Android生成带LOGO图片二维码的方法
- Android开源库之使用ZXing开源库生成二维码及识别本地二维码图片
- Android利用ZXing生成带LOGO的二维码图片
- Android生成带图片的二维码
- Android二维码图片生成和二维码扫描软件的设计
- Android扫一扫(Zxing),闪光灯,生成二维码图片,解析二维码(条码)等
- android 生成二维码图片
- Android生成带图片的二维码
- Android平台利用ZXING生成二维码图片
- Android生成二维码--拍照或从相册选取图片
- Android 使用zxing包生成中间有logo的二维码图片
- Android生成二维码--保存和分享二维码图片
- android 获取网络图片生成二维码
- Android生成带LOGO图片二维码的方法
- Android平台利用ZXING生成二维码图片
- 基于ZXing Android实现生成二维码图片和相机扫描二维码图片即时解码的功能