我想从字符串和搜索stackoverflow生成qr代码,但是当我使用这些代码时,我有错误“意外......”! 这是我的代码。 任何人都可以说我错了吗? 除了我需要另一个代码,从qr代码给我字符串... 感谢
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
public class StringtoQRActivity extends Activity {
public final static int WHITE = 0xFFFFFFFF;
public final static int BLACK = 0xFF000000;
public final static int WIDTH = 500;
public final static String STR = "amirahmad";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageView = (ImageView) findViewById(R.id.QRcode);
Bitmap bitmap = encodeToQrCode(STR, WIDTH, WIDTH);
imageView.setImageBitmap(bitmap);
}
public static Bitmap encodeToQrCode(String text, int width, int height) {
QRCodeWriter writer = new QRCodeWriter();
BitMatrix matrix = null;
try {
matrix = writer.encode(text, BarcodeFormat.QR_CODE, 100, 100);
}
catch (WriterException ex) {
ex.printStackTrace();
}
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bmp.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
return bmp;
}
}