Android之二维码生成实现
2017-01-16 14:33
113 查看
-----------------转载请注明出处:http://blog.csdn.net/android_cll
一:先上效果图、
![](https://img-blog.csdn.net/20170116143553387?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQW5kcm9pZF9DbGw=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
二:实现步骤、
1.xml的实现、
2.二维码外围转角、
3.activity的实现、
就只需简单几步就OK了!不会的可以学习一下,大神就别喷了、、、、
一:先上效果图、
二:实现步骤、
1.xml的实现、
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f0f0f0" android:orientation="vertical"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="49dp" android:background="#ffffff"> <ImageView android:id="@+id/imgfh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:padding="5dp" android:src="@drawable/farm_nav_fanhui" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="优惠券" android:textColor="#333333" android:textSize="18dp" /> </RelativeLayout> <View android:layout_width="fill_parent" android:layout_height="0.5dp" android:background="#d9dad7" /> <View android:layout_width="fill_parent" android:layout_height="9dp" android:background="#f0f0f0" /> <View android:layout_width="fill_parent" android:layout_height="0.5dp" android:background="#e6e6e6" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="49dp" android:background="#ffffff"> <ImageView android:id="@+id/juan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="12dp" android:src="@drawable/haojuan_saoma_icon" /> <TextView android:id="@+id/titlezhe" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/juan" android:text="七天连锁酒店7.5折优惠券" android:textColor="#262626" android:textSize="16dp" /> </RelativeLayout> <View android:layout_width="fill_parent" android:layout_height="0.5dp" android:background="#e6e6e6" /> <LinearLayout android:layout_width="match_parent" android:layout_height="36dp" android:background="#ffffff" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="12dp" android:text="有效期:" android:textColor="#808080" android:textSize="14dp" /> <TextView android:id="@+id/yxtime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="2016.12.30-2017.1.16" android:textColor="#808080" android:textSize="14dp" /> </LinearLayout> <View android:layout_width="fill_parent" android:layout_height="0.5dp" android:background="#e6e6e6" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="228dp" android:background="#ffffff"> <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="165dp" android:layout_height="165dp" android:layout_centerHorizontal="true" android:layout_marginTop="22dp" android:background="@drawable/sjzsewm"> <ImageView android:id="@+id/img" android:layout_width="150dp" android:layout_height="150dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> </RelativeLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/relativeLayout1" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="向商家出示扫码即可消费" android:textColor="#343434" android:textSize="14dp" /> </RelativeLayout> <View android:layout_width="fill_parent" android:layout_height="0.5dp" android:background="#e6e6e6" /> </LinearLayout>
2.二维码外围转角、
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 背景颜色 --> <solid android:color="#ffffff" /> <!-- 控制边界线颜色和大小 --> <stroke android:width="1dp" android:color="#e6e6e6" /> <!-- 控制圆角大小 --> <corners android:radius="4dp" /> </shape>
3.activity的实现、
package com.zjtd.bzcommunity.activity; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.os.Environment; import android.view.Gravity; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.zjtd.bzcommunity.R; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Calendar; import java.util.Date; /** * Created by Administrator on 2017/1/16. * 优惠券扫描 */ public class YhqsmActivity extends Activity implements View.OnClickListener { private ImageView imgfh;//返回键 private TextView titlezhe;//title和打折类型 private TextView yxtime;//有效期 private ImageView result = null;//二维码 private Bitmap bmp = null;//bitmap private String content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.yhqsm_layout); initlayout(); } /** * 实例化 */ private void initlayout() { imgfh = (ImageView) findViewById(R.id.imgfh); titlezhe = (TextView) findViewById(R.id.titlezhe); yxtime = (TextView) findViewById(R.id.yxtime); result = (ImageView) findViewById(R.id.img); imgfh.setOnClickListener(this); content = "你来打我呀!"; try { bmp = createBitmap(Create2DCode(content)); } catch (WriterException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } result.setImageBitmap(bmp); ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, baos1); try { Writetemp(baos1.toByteArray()); } catch (IOException e) { e.printStackTrace(); } } @Override public void onClick(View v) { switch (v.getId()) { //返回键 case R.id.imgfh: finish(); break; } } public Bitmap Create2DCode(String str) throws WriterException, UnsupportedEncodingException { BitMatrix matrix = new MultiFormatWriter().encode( new String(str.getBytes("GBK"), "ISO-8859-1"), BarcodeFormat.QR_CODE, 300, 300); int width = matrix.getWidth(); int height = matrix.getHeight(); int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (matrix.get(x, y)) { pixels[y * width + x] = 0xff000000; } } } int[] colors = {R.color.white}; Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } public void Write(byte[] b) throws IOException { File cacheFile = null; if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { File sdCardDir = Environment.getExternalStorageDirectory(); long time = Calendar.getInstance().getTimeInMillis(); String fileName = time + ".png"; File dir = new File(sdCardDir.getCanonicalPath() + "/zibuyu/"); if (!dir.exists()) { dir.mkdirs(); } cacheFile = new File(dir, fileName); } Toast toast = Toast.makeText(getApplicationContext(), "哈哈哈哈", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); LinearLayout toastView = (LinearLayout) toast.getView(); ImageView imageCodeProject = new ImageView(getApplicationContext()); toastView.addView(imageCodeProject, 0); toast.show(); BufferedOutputStream bos = null; try { bos = new BufferedOutputStream(new FileOutputStream(cacheFile)); bos.write(b, 0, b.length); bos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void Writetemp(byte[] b) throws IOException { File cacheFile = null; if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { File sdCardDir = Environment.getExternalStorageDirectory(); Date curDate = new Date(System.currentTimeMillis()); String fileName = "temp.jpg"; File dir = new File(sdCardDir.getCanonicalPath() + "/zibuyu/temp"); if (!dir.exists()) { dir.mkdirs(); } cacheFile = new File(dir, fileName); } BufferedOutputStream bos = null; try { bos = new BufferedOutputStream(new FileOutputStream(cacheFile)); bos.write(b, 0, b.length); bos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private Bitmap createBitmap(Bitmap src) { if (src == null) { return null; } Paint paint = new Paint(); paint.setColor(Color.WHITE); paint.setAntiAlias(true); int w = 300; int h = 300; Bitmap newb = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas cv = new Canvas(newb); cv.drawColor(Color.WHITE); cv.drawBitmap(src, 0, 0, null); cv.save(Canvas.ALL_SAVE_FLAG); cv.restore(); return newb; } }
就只需简单几步就OK了!不会的可以学习一下,大神就别喷了、、、、
相关文章推荐
- Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系
- 在android上实现二维码生成和扫描
- cocos2dx-lua在android上实现生成及扫描二维码
- Android 使用Zxing实现二维码的生成,扫描
- Android Zxing实现生成二维码功能
- Android 实现二维码生成和解析
- Android实例-实现扫描二维码并生成二维码(XE8+小米5)
- Android 使用Zxing实现二维码的生成,扫描
- Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系
- Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系
- Android基于Zxing实现二维码,条形码扫描和生成二维码
- Android 使用Zxing实现二维码的生成,扫描
- Android平台生成二维码并实现扫描 & 识别功能
- Android快速实现二维码解析及生成模块(Zxing框架)
- android 中使用Zxing实现二维码的解码和二维码的生成
- Android 基于google Zxing实现二维码的生成,识别和长按识别的效果
- Android基于Zxing实现扫描和生成二维码
- Android二维码解析和生成(菜鸟的福利来了,70多行代码实现)
- Android开发之利用ZXing库实现二维码的生成
- Android 使用Zxing实现二维码的生成,扫描