要下载一个类库core.jar,在我的资源文件里点击打开链接
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
public class ErweimashezhiActivity extends Activity implements SurfaceHolder.Callback{
Button zuotiananniu,youbiananniu,fenxiangwodemingpian,jinruwodevzu;
TextView zhongjianzifu;
private SurfaceView mSurfaceView01;
private SurfaceHolder mSurfaceHolder01;
final String imageFileName = "2DCode.png";
FileOutputStream fos = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_erweimashezhi);
String xinxi = "手机号:"+"123456789"+"\n邮箱:"+"a@a.com"+"\n公司:"+"mfo"
+"\n地址:"+"xxxxxxx12345"+"\n固话:"+"123456"+"\n邮编:"+"123456";
System.out.println(xinxi);
try {
Bitmap bitmap=Create2DCode(xinxi);
if(null != bitmap) {//将二维码图像保存到文件
File file = new File(Environment.getExternalStorageDirectory(), imageFileName);
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 0, fos);
}
ImageView imagview = (ImageView) findViewById(R.id.erweimatupian);
imagview.setImageBitmap(bitmap);
// Canvas canvas = new Canvas(bitmap);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public Bitmap Create2DCode(String str) throws WriterException, UnsupportedEncodingException {
//生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
str=new String(str.getBytes("UTF-8"),"ISO-8859-1");//
BitMatrix matrix = new MultiFormatWriter().encode(str,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;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
//通过像素数组生成bitmap,具体参考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
public class ErweimashezhiActivity extends Activity implements SurfaceHolder.Callback{
Button zuotiananniu,youbiananniu,fenxiangwodemingpian,jinruwodevzu;
TextView zhongjianzifu;
private SurfaceView mSurfaceView01;
private SurfaceHolder mSurfaceHolder01;
final String imageFileName = "2DCode.png";
FileOutputStream fos = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_erweimashezhi);
String xinxi = "手机号:"+"123456789"+"\n邮箱:"+"a@a.com"+"\n公司:"+"mfo"
+"\n地址:"+"xxxxxxx12345"+"\n固话:"+"123456"+"\n邮编:"+"123456";
System.out.println(xinxi);
try {
Bitmap bitmap=Create2DCode(xinxi);
if(null != bitmap) {//将二维码图像保存到文件
File file = new File(Environment.getExternalStorageDirectory(), imageFileName);
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 0, fos);
}
ImageView imagview = (ImageView) findViewById(R.id.erweimatupian);
imagview.setImageBitmap(bitmap);
// Canvas canvas = new Canvas(bitmap);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public Bitmap Create2DCode(String str) throws WriterException, UnsupportedEncodingException {
//生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
str=new String(str.getBytes("UTF-8"),"ISO-8859-1");//
BitMatrix matrix = new MultiFormatWriter().encode(str,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;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
//通过像素数组生成bitmap,具体参考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}