我是从新大陆物联网题里面精心提出来的生成码的 竞赛只需要打印二维码就行
我一看这个不仅能生成二位 还能生成其他的 这不知道是什么码 写起来吧 万一后工作用到怎么办
只需要两个 Java程
一个是生成码的 线程
另一个就是控制Activity 比较简单
这里还是借助ZXing的开源代码 所以还是需要这个库的可以自己下载 我也会提供源码的
1 . 来吧先来这个主要的生成线程吧
有几个生疏的方法或类自己去查API吧 !!!
</pre><p></p><pre>
<span style="font-size:18px;">package com.zbvc.demoqr;
<span style="color:#ffcc33;">import java.util.Hashtable;
import android.graphics.Bitmap;
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.datamatrix.DataMatrixReader;
import com.google.zxing.oned.Code39Writer;
import com.google.zxing.pdf417.encoder.PDF417Writer;
import com.google.zxing.qrcode.QRCodeWriter;
</span>
<span style="color:#009900;">/**
* 条码
* @author Administrator
*
*/</span>
public class Barcode {
<span style="color:#33cc00;">//串口句柄值
// public static int com_fd = -1;</span>
private char[] printData=null;
<span style="color:#33cc00;"> //数据头
private char head=0xaa;
</span>
<span style="color:#33cc00;"> /**
* 用字符串生成二维码
* @param str
* @author
* @param select //选择需要打印条形码的格式
* @return
* @throws WriterException
*/</span>
public Bitmap CreateBarcode(String input, int codeStyle) throws WriterException {
printData=null;
Hashtable<EncodeHintType,String> hints = new Hashtable<EncodeHintType,String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");<span style="color:#33cc00;">//注意的地方,这里设置中文编码为UTF-8 不然无法解码中文
//生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败</span>
BitMatrix matrix =null;
int width = 0;
int height = 0;
byte [] widthArr=null;
byte []heightArr=null;
switch (codeStyle) {
<span style="color:#33cc00;">//QR码</span>
case 0:
matrix=new QRCodeWriter().encode(input, BarcodeFormat.QR_CODE, 320, 320,hints);
width=matrix.getWidth();
height=matrix.getHeight();
widthArr=shortToByteArray(width);
heightArr=shortToByteArray(height);
break;
<span style="color:#33cc00;">//PDF417码</span>
case 1:
matrix=new PDF417Writer().encode(input, BarcodeFormat.PDF_417, 240, 160,hints);
width=240;
height=160;
widthArr=shortToByteArray(width);
heightArr=shortToByteArray(height);
break;
<span style="color:#33cc00;">//EAN_13码</span>
case 2:
matrix = new MultiFormatWriter().encode(input,
BarcodeFormat.EAN_13, 320,120, null);
width = matrix.getWidth();
height = matrix.getHeight();
widthArr=shortToByteArray(width);
heightArr=shortToByteArray(height);
break;
<span style="color:#33cc00;">//CODE_39码</span>
case 3:
matrix = new Code39Writer().encode(input,
BarcodeFormat.CODE_39, 360,80, null);
width = matrix.getWidth();
height = matrix.getHeight();
widthArr=shortToByteArray(width);
heightArr=shortToByteArray(height);
break;
//CODE_39码
default:
break;
}
//如果是解码,将EncodeHintType写为DecodeHintType即可。
<span style="color:#33ff33;">// BitMatrix matrix = new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE, 240, 300);</span>
<span style="color:#33cc00;">//二维矩阵转为一维像素数组,也就是一直横着排了</span>
int[] pixels = new int[width * height];;
printData=new char[width*height/8+6];
printData[0]=head;
printData[1]=function;
printData[2]=(char) widthArr[1];
printData[3]=(char) widthArr[0];
printData[4]=(char) heightArr[1];
printData[5]=(char) heightArr[0];
if (codeStyle==1) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if(matrix.get(y, x)){
pixels[y* width + x] = 0xff000000;
}
}
}
}else {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if(matrix.get(x, y)){
pixels[y * width + x] = 0xff000000;
}
}
}
}
<span style="color:#33cc00;">//将点阵赋值到数组</span>
for (int i = 0; i < width*height/8; i++) {
byte b=0;
for (int j = 0; j < 8; j++) {
b<<=1;
if (i*8+j>=pixels.length) {
continue;
}
b|=(pixels[i*8+j]==0?0:1);
}
printData[i+6]=(char) b;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
//通过像素数组生成bitmap,具体参考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
<span style="color:#33cc00;">/**
* 长度int转化成两个byte【】
* @param s
* @return
*/</span>
private byte[] shortToByteArray(int s) {
byte l=(byte) (s&0xff);
byte h=(byte) ((s>>>8)&0xff);
return new byte[]{h,l};
}
<span style="color:#33ff33;">
/***************************
* 获取序列化后,待打印的字符数组
* @param 无
* @return 返回序字符数组
*****************************/</span>
public char[] getPrintData() {
return printData;
}
} </span>
因为利用了库所以这是比较简单的
2 .在看Activity的Java前先看看UI 可以了解一下 我定义的的 ID
<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/aaaa0">
<ImageView
android:id="@+id/imageView1"
android:layout_width="475dp"
android:layout_height="460dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/spinner1"
android:text="请选择生成格式"
android:textSize="20dp"
/>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_marginBottom="74dp"
android:background="#66ff66"
android:text="Print" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="23dp"
android:ems="10"
android:gravity="center_horizontal"
android:hint="请输入您要生成信息"
android:textColor="#339999" >
<requestFocus />
</EditText>
<span style="color:#ff0000;"> <Spinner
android:id="@+id/spinner1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_above="@+id/editText1"
android:layout_alignParentRight="true"
android:layout_marginBottom="30dp"
</span><span style="color:#009900;"> android:entries="@array/secele"</span><span style="color:#ff0000;"> /> </span><span style="color:#009900;">//我们用到了Spinner控件就去新建一个array</span>
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#00cccc"
android:text="Back" />
</RelativeLayout></span>
这是array下的文件
<span style="font-size:14px;color:#ff6600;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="secele">
<item >请选择</item>
<item >QR_CODE</item>
<item >PFD_417</item>
<item >CODE_36只能为数字</item>
</string-array>
</resources></span>
3 . 看我的Activity文件
<span style="font-size:18px;"><span style="color:#cc0000;">package com.zbvc.demoqr;</span></span>
<span style="font-size:18px;">
<span style="color:#ffcc00;">import com.google.zxing.WriterException;
import android.app.Activity;
import android.content.res.Resources.NotFoundException;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.renderscript.Type;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
</span>
public class QrActivity extends Activity {
private String port = "4000";
private Barcode barcode;
private ImageView ivQR;
private Bitmap bitmap;
private Button print;
private Button back;
private EditText input;
private Spinner typeChoole;
protected int tag;
@Override
protected void onCreate(Bundle savedInstanceState) {
<span style="color:#009900;">// TODO Auto-generated method stub</span>
super.onCreate(savedInstanceState);
setContentView(R.layout.qripshow);
setTitle("二维码—IP地址");
input = (EditText)findViewById(R.id.editText1);
back = (Button) findViewById(R.id.button2);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
<span style="color:#009900;">// TODO Auto-generated method stub</span>
finish();
}
});
initComponet();
}
<span style="color:#33cc00;">/**
* 初始化控件
*/</span>
private void initComponet() {
typeChoole= (Spinner)findViewById(R.id.spinner1);
typeChoole.setOnItemSelectedListener(new OnItemSelectedListener()
{
<span style="color:#33cc00;">//@Override</span>
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
<span style="color:#33cc00;">// TODO Auto-generated method stub
//String value = arg0.get</span>
String value=arg0.getItemAtPosition(arg2).toString();
if(value.equals("QR_CODE"))
{
tag = 0;
}else
if(value.equals("PFD_417"))
{
tag = 1;
}
else if(value.equals("CODE_36只能为数字"))
{
tag = 3;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
<span style="color:#33cc00;">// TODO Auto-generated method stub</span>
}
});
print = (Button)findViewById(R.id.button1);
input = (EditText)findViewById(R.id.editText1);
ivQR = (ImageView) findViewById(R.id.imageView1);
final String data = input.getText().toString();
print.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String data = input.getText().toString();
barcode = new Barcode();
try
{
bitmap = barcode.CreateBarcode(data,tag);//打印德 内容是IP 选择的条形码格式
ivQR.setImageBitmap(bitmap);
} catch (WriterException e) {
<span style="color:#33cc00;">// TODO Auto-generated catch block</span>
e.printStackTrace();
}
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { /<span style="color:#33cc00;">/平板上的返回键</span>
<span style="color:#33cc00;">// TODO Auto-generated method stub</span>
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
finish();
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
}
</span>
看一下效果吧
我的源码 http://download.csdn.net/
detail/csdnhejingzhou/9262323