在项目中我是想实现通过摄像头拍照,然后将拍照的照片生成qrcode图片,然后再解析里面的文字信息。。不过,目前只找到将字符串生成qrcode图片的代码。。。
要生成qrcode二维码需要在项目添加zxing1.6 的 core.jar包。
Activity:
今天的文章
将文字生成qrcode二维码分享到此就结束了,感谢您的阅读。
要生成qrcode二维码需要在项目添加zxing1.6 的 core.jar包。
Activity:
package com.create.qrcode;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Base64;
public class CreateQRCodeActivity extends Activity {
private Bitmap bitmap;
private Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// String content = "E67";
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
// String content = GetImageStr("/mnt/sdcard/test.jpg");
// String str = hex2bin(content);
String str = "testing...";
System.out.println("str : " + str);
long a = System.currentTimeMillis();
bitmap = CreateQRCodeActivity.this.createBitmap(str);
final long b = System.currentTimeMillis();
System.out.println("bitmap 耗时:" + (b - a) + ",bitmap : " + bitmap);
mHandler.post(new Runnable() {
public void run() {
setContentView(new BitmapView(CreateQRCodeActivity.this, bitmap));
long c = System.currentTimeMillis();
System.out.println("setContentview 耗时:" + (c - b));
}
});
}
}).start();
}
/
* 十六进制转换字符串
* @param hex String 十六进制
* @return String 转换后的字符串
*/
public static String hex2bin(String hex) {
String digital = "0ABCDEF";
char[] hex2char = hex.toCharArray();
byte[] bytes = new byte[hex.length() / 2];
int temp;
for (int i = 0; i < bytes.length; i++) {
temp = digital.indexOf(hex2char[2 * i]) * 16;
temp += digital.indexOf(hex2char[2 * i + 1]);
bytes[i] = (byte) (temp & 0xff);
}
return new String(bytes);
}
public static String GetImageStr(String imgFilePath) {
if (imgFilePath == null || imgFilePath == "") {
return "";
}
File file = new File(imgFilePath);
if (!file.exists()) {
return "";
}
byte[] data = null;
try {
InputStream in = new FileInputStream(imgFilePath);
data = getByte(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
String imageString = Base64.encodeToString(data, Base64.DEFAULT);
return imageString;
}
public static byte[] getByte(InputStream in) {
if (in == null) {
return null;
}
int sumSize = 0;
List<byte[]> totalBytes = new ArrayList<byte[]>();
byte[] buffer = new byte[1024];
int length = -1;
try {
while ((length = in.read(buffer)) != -1) {
sumSize += length;
byte[] tmp = new byte[length];
System.arraycopy(buffer, 0, tmp, 0, length);
totalBytes.add(tmp);
}
byte[] data = new byte[sumSize];
int start = 0;
for (byte[] tmp : totalBytes) {
System.arraycopy(tmp, 0, data, start, tmp.length);
start += tmp.length;
}
return data;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public Bitmap createBitmap(String str) {
try {
BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 300, 300);
int width = matrix.width;
int height = matrix.height;
int[] pixels = new int[width*height];
for(int y = 0; y<width; ++y){
for(int x = 0; x<height; ++x){
if(matrix.get(x, y)){
pixels[y*width+x] = 0xff000000; // black pixel
} else {
pixels[y*width+x] = 0xffffffff; // white pixel
}
}
}
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bmp.setPixels(pixels, 0, width, 0, 0, width, height);
return bmp;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
}
BitmapView.java
package com.create.qrcode;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
public class BitmapView extends View {
Bitmap bitmap;
public BitmapView(Context context, Bitmap bitmap) {
super(context);
this.bitmap = bitmap;
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setFilterBitmap(true);
double aspectRatio = ((double)bitmap.getWidth())/bitmap.getHeight();
Rect dest = new Rect(0,0,this.getWidth(), (int)(this.getHeight()/aspectRatio));
canvas.drawBitmap(bitmap, null, dest, paint);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/83403.html