网站建设工作小组推进表,求职简历模板电子版免费,wordpress 安装第二步,网站设计和营销由于微信的推出二维码走进了我们的生活#xff0c;并且越来越多的人们正在发挥着自己的想象力去使用它#xff0c;来方便我们的生活#xff0c;我曾经听说过一个笑话#xff0c;当我们死后#xff0c;墓碑上不再有墓志铭#xff0c;而会出现一个记录你一生信息的二维码并且越来越多的人们正在发挥着自己的想象力去使用它来方便我们的生活我曾经听说过一个笑话当我们死后墓碑上不再有墓志铭而会出现一个记录你一生信息的二维码当人们走到你们的墓碑前掏出手机扫一扫就可以看到你一生的丰功伟绩。这是不是很有意思我都认这会在不久的将来成为现实哈哈玩笑说完了下面我们来一起学习一下如何在Android开发中让二维码为我们服务。 本篇我将会带领朋友们实现一个记录个人基本信息的二维码设计思路对于搞过算法的大牛们这里要让你们失望了对于二维码生成的算法本人才疏学浅尚且无法为大家分享本篇事例的实现我们将借助core.jar实现对于这个jar包的下载我为大家提供一个链接方便大家学习使用http://pan.baidu.com/s/1bnGZoF9 准备好我们的jar包后我们开始今天的设计第一步创建工程导入jar包 在我们的集成开发环境中创建一个Android工程项目为我们今天事例的设计做铺垫。创建好工程后将我们刚刚下载好的jar包导入到我们的工程中Ctrlc我们的jar包在我们的工程目录下找到libs文件夹Ctrlv然后呢就是通过集成开发环境将我们的jar包导入到工程。 第二步创建我们的布局文件 RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:paddingBottomdimen/activity_vertical_marginandroid:paddingLeftdimen/activity_horizontal_marginandroid:paddingRightdimen/activity_horizontal_marginandroid:paddingTopdimen/activity_vertical_margintools:context.MainActivity LinearLayoutandroid:layout_widthfill_parentandroid:layout_heightwrap_contentandroid:orientationverticalandroid:idid/ll!-- 用于展示我们创建的二维码 --ImageViewandroid:idid/imgCodeandroid:layout_width100dipandroid:layout_height100dipandroid:layout_gravitycenter_horizontal /!-- 公司 --LinearLayoutandroid:layout_widthfill_parentandroid:layout_heightwrap_contentandroid:orientationhorizontal TextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text公司 /EditTextandroid:idid/etCompanyandroid:layout_widthfill_parentandroid:layout_heightwrap_contentandroid:hint选填 //LinearLayout!-- 电话 --LinearLayoutandroid:layout_widthfill_parentandroid:layout_heightwrap_contentandroid:orientationhorizontal TextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text电话 /EditTextandroid:idid/etPhoneandroid:layout_widthfill_parentandroid:layout_heightwrap_contentandroid:hint选填 //LinearLayout!-- 邮箱 --LinearLayoutandroid:layout_widthfill_parentandroid:layout_heightwrap_contentandroid:orientationhorizontal TextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text邮箱 /EditTextandroid:idid/etEmailandroid:layout_widthfill_parentandroid:layout_heightwrap_contentandroid:hint选填 //LinearLayout!-- 网址 --LinearLayoutandroid:layout_widthfill_parentandroid:layout_heightwrap_contentandroid:orientationhorizontal TextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text网址 /EditTextandroid:idid/etWebandroid:layout_widthfill_parentandroid:layout_heightwrap_contentandroid:texthttp:// //LinearLayoutButton android:idid/but android:layout_widthfill_parent android:layout_heightwrap_content android:text生成二维码//LinearLayout/RelativeLayout 第三步编辑我们Activity: 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;//宽度值影响中间图片大小Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获得资源图片可改成获取本地图片或拍照获取图片logoBitmapFactory.decodeResource(super.getResources(), R.drawable.ic_launcher);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() {Overridepublic void onClick(View v) {String companyetCompany.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 bmcreateCode(contents, logo, BarcodeFormat.QR_CODE);ImageView img(ImageView)findViewById(R.id.imgCode) ;//将二维码在界面中显示img.setImageBitmap(bm);} catch (WriterException e) {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();//HashtableEncodeHintType, String hst new HashtableEncodeHintType, 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);// 通过像素数组生成bitmapbitmap.setPixels(pixels, 0, width, 0, 0, width, height);return bitmap;}} 上面的代码注释已经非常详细这里我再简单说几句这部分代码分为上下两部分上部分都是我们经常使用的下部分则是我们二维码创建的重点。ok到这里我们的效果就实现了最后上一张效果图