本文整理汇总了Java中com.google.zxing.WriterException类的典型用法代码示例。如果您正苦于以下问题:Java WriterException类的具体用法?Java WriterException怎么用?Java WriterException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WriterException类属于com.google.zxing包,在下文中一共展示了WriterException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createQRCode
import com.google.zxing.WriterException; //导入依赖的package包/类
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
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] = BLACK;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
示例2: setQRCode
import com.google.zxing.WriterException; //导入依赖的package包/类
/**显示二维码
*/
protected void setQRCode() {
runThread(TAG + "setQRCode", new Runnable() {
@Override
public void run() {
try {
qRCodeBitmap = EncodingHandler.createQRCode(Constant.APP_DOWNLOAD_WEBSITE
, (int) (2 * getResources().getDimension(R.dimen.qrcode_size)));
} catch (WriterException e) {
e.printStackTrace();
Log.e(TAG, "initData try {Bitmap qrcode = EncodingHandler.createQRCode(contactJson, ivContactQRCodeCode.getWidth());" +
" >> } catch (WriterException e) {" + e.getMessage());
}
runUiThread(new Runnable() {
@Override
public void run() {
ivAboutQRCode.setImageBitmap(qRCodeBitmap);
}
});
}
});
}
示例3: encode
import com.google.zxing.WriterException; //导入依赖的package包/类
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) throws WriterException {
if (contents.length() == 0) {
throw new IllegalArgumentException("Found empty contents");
} else if (format != BarcodeFormat.QR_CODE) {
throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
} else if (width < 0 || height < 0) {
throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height);
} else {
ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
int quietZone = 4;
if (hints != null) {
ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION);
if (requestedECLevel != null) {
errorCorrectionLevel = requestedECLevel;
}
Integer quietZoneInt = (Integer) hints.get(EncodeHintType.MARGIN);
if (quietZoneInt != null) {
quietZone = quietZoneInt.intValue();
}
}
return renderResult(Encoder.encode(contents, errorCorrectionLevel, hints), width, height, quietZone);
}
}
示例4: encodeFromTextExtras
import com.google.zxing.WriterException; //导入依赖的package包/类
private void encodeFromTextExtras(Intent intent) throws WriterException {
// Notice: Google Maps shares both URL and details in one text, bummer!
String theContents = ContactEncoder.trim(intent.getStringExtra(Intent.EXTRA_TEXT));
if (theContents == null) {
theContents = ContactEncoder.trim(intent.getStringExtra("android.intent.extra.HTML_TEXT"));
// Intent.EXTRA_HTML_TEXT
if (theContents == null) {
theContents = ContactEncoder.trim(intent.getStringExtra(Intent.EXTRA_SUBJECT));
if (theContents == null) {
String[] emails = intent.getStringArrayExtra(Intent.EXTRA_EMAIL);
if (emails != null) {
theContents = ContactEncoder.trim(emails[0]);
} else {
theContents = "?";
}
}
}
}
// Trim text to avoid URL breaking.
if (theContents == null || theContents.isEmpty()) {
throw new WriterException("Empty EXTRA_TEXT");
}
contents = theContents;
// We only do QR code.
format = BarcodeFormat.QR_CODE;
if (intent.hasExtra(Intent.EXTRA_SUBJECT)) {
displayContents = intent.getStringExtra(Intent.EXTRA_SUBJECT);
} else if (intent.hasExtra(Intent.EXTRA_TITLE)) {
displayContents = intent.getStringExtra(Intent.EXTRA_TITLE);
} else {
displayContents = contents;
}
title = activity.getString(R.string.contents_text);
}
示例5: updateQrCode
import com.google.zxing.WriterException; //导入依赖的package包/类
private void updateQrCode(String qrData) {
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
wm.getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
int qrCodeDimension = screenWidth > screenHeight ? screenHeight : screenWidth;
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(qrData, null,
Contents.Type.TEXT, BarcodeFormat.QR_CODE.toString(), qrCodeDimension);
try {
mBitmap = qrCodeEncoder.encodeAsBitmap();
mImageView.setImageBitmap(mBitmap);
mImageView.setVisibility(View.VISIBLE);
mErrorView.setVisibility(View.GONE);
} catch (WriterException e) {
e.printStackTrace();
mErrorView.setText("Error: "+e.getMessage());
mErrorView.setVisibility(View.VISIBLE);
mImageView.setVisibility(View.GONE);
}
}
示例6: encodeAsBitmap
import com.google.zxing.WriterException; //导入依赖的package包/类
public Bitmap encodeAsBitmap() throws WriterException {
if (!encoded) return null;
Map<EncodeHintType, Object> hints = null;
String encoding = guessAppropriateEncoding(contents);
if (encoding != null) {
hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = writer.encode(contents, format, dimension, dimension, hints);
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;
}
示例7: createQRCode
import com.google.zxing.WriterException; //导入依赖的package包/类
public static Bitmap createQRCode(String str,int widthAndHeight) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
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] = BLACK;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
示例8: createQRCode
import com.google.zxing.WriterException; //导入依赖的package包/类
public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
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] = BLACK;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
示例9: createQRCode
import com.google.zxing.WriterException; //导入依赖的package包/类
private Bitmap createQRCode(String str, int widthAndHeight)
throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");// 使用utf8编码
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, hints);// 这里需要把hints传进去,否则会出现中文乱码
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] = BLACK;
} else {// 其他部分则使用白色
pixels[y * width + x] = WHITE;
}
}
}
//生成bitmap
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
示例10: create
import com.google.zxing.WriterException; //导入依赖的package包/类
public static @NonNull Bitmap create(String data) {
try {
BitMatrix result = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, 512, 512);
Bitmap bitmap = Bitmap.createBitmap(result.getWidth(), result.getHeight(), Bitmap.Config.ARGB_8888);
for (int y = 0; y < result.getHeight(); y++) {
for (int x = 0; x < result.getWidth(); x++) {
if (result.get(x, y)) {
bitmap.setPixel(x, y, Color.BLACK);
}
}
}
return bitmap;
} catch (WriterException e) {
Log.w(TAG, e);
return Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
}
}
示例11: printQRCodeInTerminal
import com.google.zxing.WriterException; //导入依赖的package包/类
/**
* 关于Ansi Color的信息,参考:http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
*
* @param text
*/
public static void printQRCodeInTerminal(String text) {
int qrcodeWidth = 400;
int qrcodeHeight = 400;
HashMap<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
try {
BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, qrcodeWidth, qrcodeHeight, hints);
// 将像素点转成格子
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
for (int y = 30; y < height - 30; y += 10) {
for (int x = 30; x < width - 20; x += 10) {
boolean isBlack = bitMatrix.get(x, y);
System.out.print(isBlack ? " " : "\u001b[47m \u001b[0m");
}
System.out.println();
}
}
catch (WriterException e) {
e.printStackTrace();
}
}
示例12: makeTypeInfoBits
import com.google.zxing.WriterException; //导入依赖的package包/类
static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits)
throws WriterException {
if (!QRCode.isValidMaskPattern(maskPattern)) {
throw new WriterException("Invalid mask pattern");
}
int typeInfo = (ecLevel.getBits() << 3) | maskPattern;
bits.appendBits(typeInfo, 5);
int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY);
bits.appendBits(bchCode, 10);
BitArray maskBits = new BitArray();
maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15);
bits.xor(maskBits);
if (bits.getSize() != 15) { // Just in case.
throw new WriterException("should not happen but we got: " + bits.getSize());
}
}
示例13: createQrCode
import com.google.zxing.WriterException; //导入依赖的package包/类
@Nullable
static Bitmap createQrCode(DisplayMetrics dm, String input) {
int smallestDimen = Math.min(dm.widthPixels, dm.heightPixels);
try {
// Generate QR code
final BitMatrix encoded = new QRCodeWriter().encode(
input, QR_CODE, smallestDimen, smallestDimen);
// Convert QR code to Bitmap
int width = encoded.getWidth();
int height = encoded.getHeight();
int[] pixels = new int[width * height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
pixels[y * width + x] = encoded.get(x, y) ? BLACK : WHITE;
}
}
Bitmap qr = Bitmap.createBitmap(width, height, ARGB_8888);
qr.setPixels(pixels, 0, width, 0, 0, width, height);
return qr;
} catch (WriterException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
return null;
}
}
示例14: appendKanjiBytes
import com.google.zxing.WriterException; //导入依赖的package包/类
static void appendKanjiBytes(String content, BitArray bits) throws WriterException {
try {
byte[] bytes = content.getBytes("Shift_JIS");
int length = bytes.length;
for (int i = 0; i < length; i += 2) {
int byte2 = bytes[i + 1] & 255;
int code = ((bytes[i] & 255) << 8) | byte2;
int subtracted = -1;
if (code >= 33088 && code <= 40956) {
subtracted = code - 33088;
} else if (code >= 57408 && code <= 60351) {
subtracted = code - 49472;
}
if (subtracted == -1) {
throw new WriterException("Invalid byte sequence");
}
bits.appendBits(((subtracted >> 8) * 192) + (subtracted & 255), 13);
}
} catch (UnsupportedEncodingException uee) {
throw new WriterException(uee);
}
}
示例15: appendKanjiBytes
import com.google.zxing.WriterException; //导入依赖的package包/类
static void appendKanjiBytes(String content, BitArray bits) throws WriterException {
byte[] bytes;
try {
bytes = content.getBytes("Shift_JIS");
} catch (UnsupportedEncodingException uee) {
throw new WriterException(uee);
}
int length = bytes.length;
for (int i = 0; i < length; i += 2) {
int byte1 = bytes[i] & 0xFF;
int byte2 = bytes[i + 1] & 0xFF;
int code = (byte1 << 8) | byte2;
int subtracted = -1;
if (code >= 0x8140 && code <= 0x9ffc) {
subtracted = code - 0x8140;
} else if (code >= 0xe040 && code <= 0xebbf) {
subtracted = code - 0xc140;
}
if (subtracted == -1) {
throw new WriterException("Invalid byte sequence");
}
int encoded = ((subtracted >> 8) * 0xc0) + (subtracted & 0xff);
bits.appendBits(encoded, 13);
}
}