代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
仿微信二维码生成以及条形码生成
作者:
fuweiping?
/ 发布于
2012/11/27
/
662
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<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>(); 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 < pixels.length; i++) { pixels[i] = 0xffffffff; } for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (matrix.get(x, y)) { pixels[y * width + x] = 0xff000000; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // 通过像素数组生成bitmap,具体参考api bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } public File GetCodePath(String name) { String EXTERN_PATH = null; if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED) == true) { EXTERN_PATH = android.os.Environment.getExternalStorageDirectory() .getAbsolutePath() + "/"; File f = new File(EXTERN_PATH); if (!f.exists()) { f.mkdirs(); } } return new File(EXTERN_PATH + name); } /** * 图片两端所保留的空白的宽度 */ private int marginW = 20; /** * 条形码的编码类型 */ private BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128; /** * 生成条形码 * * @param context * @param contents * 需要生成的内容 * @param desiredWidth * 生成条形码的宽带 * @param desiredHeight * 生成条形码的高度 * @param displayCode * 是否在条形码下方显示内容 * @return */ public Bitmap creatBarcode(Context context, String contents, int desiredWidth, int desiredHeight, boolean displayCode) { Bitmap ruseltBitmap = null; if (displayCode) { Bitmap barcodeBitmap = encodeAsBitmap(contents, barcodeFormat, desiredWidth, desiredHeight); Bitmap codeBitmap = creatCodeBitmap(contents, desiredWidth + 2 * marginW, desiredHeight, context); ruseltBitmap = mixtureBitmap(barcodeBitmap, codeBitmap, new PointF( 0, desiredHeight)); } else { ruseltBitmap = encodeAsBitmap(contents, barcodeFormat, desiredWidth, desiredHeight); } return ruseltBitmap; } /** * 生成显示编码的Bitmap * * @param contents * @param width * @param height * @param context * @return */ protected Bitmap creatCodeBitmap(String contents, int width, int height, Context context) { TextView tv = new TextView(context); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); tv.setLayoutParams(layoutParams); tv.setText(contents); tv.setHeight(height); tv.setGravity(Gravity.CENTER_HORIZONTAL); tv.setWidth(width); tv.setDrawingCacheEnabled(true); tv.setTextColor(Color.BLACK); tv.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight()); tv.buildDrawingCache(); Bitmap bitmapCode = tv.getDrawingCache(); return bitmapCode; } /** * 生成条形码的Bitmap * * @param contents * 需要生成的内容 * @param format * 编码格式 * @param desiredWidth * @param desiredHeight * @return * @throws WriterException */ protected Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int desiredWidth, int desiredHeight) { final int WHITE = 0xFFFFFFFF; final int BLACK = 0xFF000000; MultiFormatWriter writer = new MultiFormatWriter(); BitMatrix result = null; try { result = writer.encode(contents, format, desiredWidth, desiredHeight, null); } catch (WriterException e) { // TODO Auto-generated catch block e.printStackTrace(); } int width = result.getWidth(); int height = result.getHeight(); int[] pixels = new int[width * height]; // All are 0, or black, by default for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } /** * 将两个Bitmap合并成一个 * * @param first * @param second * @param fromPoint * 第二个Bitmap开始绘制的起始位置(相对于第一个Bitmap) * @return */ protected Bitmap mixtureBitmap(Bitmap first, Bitmap second, PointF fromPoint) { if (first == null || second == null || fromPoint == null) { return null; } Bitmap newBitmap = Bitmap.createBitmap( first.getWidth() + second.getWidth() + marginW, first.getHeight() + second.getHeight(), Config.ARGB_4444); Canvas cv = new Canvas(newBitmap); cv.drawBitmap(first, marginW, 0, null); cv.drawBitmap(second, fromPoint.x, fromPoint.y, null); cv.save(Canvas.ALL_SAVE_FLAG); cv.restore(); return newBitmap; } /*** 仿微信二维码开始 ***/ // 图片剪切 public Bitmap cutBitmap(Bitmap mBitmap, Rect r, Bitmap.Config config) { int width = r.width(); int height = r.height(); Bitmap croppedImage = Bitmap.createBitmap(width, height, config); Canvas cvs = new Canvas(croppedImage); Rect dr = new Rect(0, 0, width, height); cvs.drawBitmap(mBitmap, r, dr, null); return croppedImage; } /*** * 合并图片 * * @param src * @param watermark * @return */ private Bitmap createBitmap(Bitmap src, Bitmap watermark) { String tag = "createBitmap"; Log.d(tag, "create a new bitmap"); if (src == null) { return null; } int w = src.getWidth(); int h = src.getHeight(); int ww = watermark.getWidth(); int wh = watermark.getHeight(); // create the new blank bitmap Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图 Canvas cv = new Canvas(newb); // draw src into cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src // 在src的中间画watermark cv.drawBitmap(watermark, w / 2 - ww / 2, h / 2 - wh / 2, null);// 设置ic_launcher的位置 // save all clip cv.save(Canvas.ALL_SAVE_FLAG);// 保存 // store cv.restore();// 存储 return newb; } /*** * 缩放图片 * * @param src * @param destWidth * @param destHeigth * @return */ private Bitmap zoomBitmap(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) / w;// 宽度缩小比例 float scaleHeight = ((float) destHeigth) / 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);// 直接按照矩阵的比例把源文件画入进行 return resizedBitmap; } }
试试其它关键字
条形码生成
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
fuweiping?
贡献的其它代码
(
1
)
.
仿微信二维码生成以及条形码生成
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3