Android仿微信二维码和条码

来源:爱站网时间:2021-01-04编辑:网友分享
绝大多数Android开发者都使用谷歌zxing扫描二维码和条形码,但很多演示的官方和在线扫描界面让人难以忍受,本文是爱站技术频道小编为大家介绍的Android仿微信二维码和条码,感兴趣的小伙伴们进入下文看看吧!

绝大多数Android开发者都使用谷歌zxing扫描二维码和条形码,但很多演示的官方和在线扫描界面让人难以忍受,本文是爱站技术频道小编为大家介绍的Android仿微信二维码和条码,感兴趣的小伙伴们进入下文看看吧!

具体内容如下

package your.QRCode.namespace;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class QRCodeTextActivityActivity extends Activity {
 /** Called when the activity is first created. */
 Button btn1 = null;
 Button btn2 = null;
 ImageView ivImageView = null;

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 btn1 = (Button) findViewById(R.id.button1);// 条形码
 btn2 = (Button) findViewById(R.id.button2);// 二维码
 ivImageView = (ImageView) findViewById(R.id.imageView1);
 final String strconteString = "c2b0f58a6f09cafd1503c06ef08ac7aeb7ddb91a602dac145551c102143e6159e385cdc294";

 btn1.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
  Bitmap mBitmap = null;
  mBitmap = creatBarcode(QRCodeTextActivityActivity.this,
   strconteString, 300, 300, true);
  if (mBitmap != null) {
   ivImageView.setImageBitmap(mBitmap);
  }
  }
 });
 btn2.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
  Bitmap mBitmap = null;
  try {
   if (!strconteString.equals("")) {
   mBitmap = Create2DCode(strconteString);

   // Bitmap bm =
   // BitmapFactory.decodeResource(getResources(),
   // R.drawable.diagnose1);
   ivImageView.setImageBitmap(createBitmap(
    mBitmap,
    zoomBitmap(BitmapFactory.decodeResource(
     getResources(), R.drawable.cccc), 100,100)));
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  }
 });
 }

 public Bitmap Create2DCode(String str) throws WriterException {
 Map hints = new HashMap();
 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
 hints.put(EncodeHintType.CHARACTER_SET, "GBK");
 // hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
 // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
 BitMatrix matrix = new MultiFormatWriter().encode(str,
  BarcodeFormat.QR_CODE, 500, 500, hints);
 int width = matrix.getWidth();
 int height = matrix.getHeight();
 // 二维矩阵转为一维像素数组,也就是一直横着排了
 int[] pixels = new int[width * height];
 for (int i = 0; i 



  


  

图片美工做下处理。貌似需要做一个描边。png透明背景 

在加两个方法

/***
 * 缩放图片并加描边
 * 
 * @param src
 * @param destWidth
 * @param destHeigth
 * @return
 */
 private Bitmap zoomBitmapBorder(Bitmap src, int destWidth, int destHeigth) {
 String tag = "lessenBitmap";
 if (src == null) {
  return null;
 }
 int w = src.getWidth();// 源文件的大小
 int h = src.getHeight();
 // calculate the scale - in this case = 0.4f
 float scaleWidth = ((float) destWidth - 4) / w;// 宽度缩小比例
 float scaleHeight = ((float) destHeigth - 4) / h;// 高度缩小比例
 Log.d(tag, "bitmap width is :" + w);
 Log.d(tag, "bitmap height is :" + h);
 Log.d(tag, "new width is :" + destWidth);
 Log.d(tag, "new height is :" + destHeigth);
 Log.d(tag, "scale width is :" + scaleWidth);
 Log.d(tag, "scale height is :" + scaleHeight);
 Matrix m = new Matrix();// 矩阵
 m.postScale(scaleWidth, scaleHeight);// 设置矩阵比例
 Bitmap resizedBitmap = Bitmap.createBitmap(src, 0, 0, w, h, m, true);// 直接按照矩阵的比例把源文件画入进行

 Bitmap newb = Bitmap.createBitmap(destWidth, destHeigth,
  Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
 Canvas cv = new Canvas(newb);
 //cv.drawColor(R.color.white);
cv.drawRGB(0,128,128);
 cv.drawBitmap(resizedBitmap, 2, 2, null);// 设置ic_launcher的位置

 // save all clip
 cv.save(Canvas.ALL_SAVE_FLAG);// 保存
 // store
 cv.restore();// 存储

 return getRoundedCornerBitmap(newb);
 }

 /**
 * 图片圆角
 * @param bitmap
 * @return
 */
 public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
 Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
  bitmap.getHeight(), Config.ARGB_8888);
 Canvas canvas = new Canvas(output);
 final int color = 0xff424242;
 final Paint paint = new Paint();
 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
 final RectF rectF = new RectF(rect);
 final float roundPx = 12;
 paint.setAntiAlias(true);
 canvas.drawARGB(0, 0, 0, 0);
 paint.setColor(color);
 canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
 canvas.drawBitmap(bitmap, rect, rect, paint);
 return output;

 }

上述是爱站技术频道小编介绍的Android仿微信二维码和条码,而微信二维码和条码的开发更加的有利于推广,同时也让项目变得更加新颖且独立。

上一篇:Android屏幕截屏的方法

下一篇:Android工作室的开发技巧

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载