本文整理汇总了Java中com.google.zxing.qrcode.QRCodeWriter.encode方法的典型用法代码示例。如果您正苦于以下问题:Java QRCodeWriter.encode方法的具体用法?Java QRCodeWriter.encode怎么用?Java QRCodeWriter.encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.zxing.qrcode.QRCodeWriter
的用法示例。
在下文中一共展示了QRCodeWriter.encode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getQR
import com.google.zxing.qrcode.QRCodeWriter; //导入方法依赖的package包/类
public static Bitmap getQR(final byte[] content, final int size) {
final QRCodeWriter writer = new QRCodeWriter();
try {
final Map<EncodeHintType, Object> encodingHints = new HashMap<>();
encodingHints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
encodingHints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
final BitMatrix encoding = writer.encode(Utils.makeQR(content), BarcodeFormat.QR_CODE, size, size, encodingHints);
final Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
bitmap.setPixel(i, j, encoding.get(i, j) ? COLOR_DARK : COLOR_LIGHT);
}
}
return bitmap;
} catch (WriterException e) {
Log.e("QRUtils", "Failed to get QR code", e);
}
return null;
}
示例2: create2dBarcodeBitmap
import com.google.zxing.qrcode.QRCodeWriter; //导入方法依赖的package包/类
public static Bitmap create2dBarcodeBitmap(String input, int size) {
try {
final QRCodeWriter barcodeWriter = new QRCodeWriter();
final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
final BitMatrix result = barcodeWriter.encode(input, BarcodeFormat.QR_CODE, size, size, hints);
final int width = result.getWidth();
final int height = result.getHeight();
final int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
final int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
}
}
final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (final Exception e) {
e.printStackTrace();
return null;
}
}
示例3: createQrCodeBitmap
import com.google.zxing.qrcode.QRCodeWriter; //导入方法依赖的package包/类
protected Bitmap createQrCodeBitmap(String input, int size) {
Log.d(Config.LOGTAG,"qr code requested size: "+size);
try {
final QRCodeWriter QR_CODE_WRITER = new QRCodeWriter();
final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
final BitMatrix result = QR_CODE_WRITER.encode(input, BarcodeFormat.QR_CODE, size, size, hints);
final int width = result.getWidth();
final int height = result.getHeight();
final int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
final int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.TRANSPARENT;
}
}
final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Log.d(Config.LOGTAG,"output size: "+width+"x"+height);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (final WriterException e) {
return null;
}
}
示例4: encode
import com.google.zxing.qrcode.QRCodeWriter; //导入方法依赖的package包/类
/**
* Encode a QR code
* @param string, the string to encode
* @param encodingOptions, encoding options
* @return A new QRPlusInfo
* @throws WriterException
*/
public static QRPlusInfo encode(String string, Map<EncodeHintType, Object> encodingOptions) throws WriterException
{
QRCodeWriter writer = new QRCodeWriter();
BitMatrix q = writer.encode(
string,
BarcodeFormat.QR_CODE, 0, 0, encodingOptions );
return new QRPlusInfo(string, q);
}
示例5: genQRCode
import com.google.zxing.qrcode.QRCodeWriter; //导入方法依赖的package包/类
/**
* 生成二维码
*
* @param url 二维码包含的URL信息
* @return Bitmap
*/
@SuppressWarnings("SuspiciousNameCombination")
private Bitmap genQRCode(String url) {
// 等待placeholder绘制完成之后调用
int width = mQRCodeImage.getMeasuredWidth();
Bitmap bitmap = null;
QRCodeWriter writer = new QRCodeWriter();
try {
BitMatrix bm = writer.encode(url, BarcodeFormat.QR_CODE, width, width);
bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
for (int i = 0; i < width; i++) {
for (int j = 0; j < width; j++) {
bitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK : Color.WHITE);
}
}
} catch (WriterException e) {
Log.w(TAG, e.toString());
}
return bitmap;
}
示例6: generateQrCodeBitmap
import com.google.zxing.qrcode.QRCodeWriter; //导入方法依赖的package包/类
private static Bitmap generateQrCodeBitmap(@NonNull final String value) throws WriterException {
final QRCodeWriter writer = new QRCodeWriter();
final int size = BaseApplication.get().getResources().getDimensionPixelSize(R.dimen.qr_code_size);
final Map<EncodeHintType, Integer> map = new HashMap<>();
map.put(EncodeHintType.MARGIN, 0);
final BitMatrix bitMatrix = writer.encode(value, BarcodeFormat.QR_CODE, size, size, map);
final int width = bitMatrix.getWidth();
final int height = bitMatrix.getHeight();
final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
final int contrastColour = ContextCompat.getColor(BaseApplication.get(), R.color.windowBackground);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : contrastColour);
}
}
return bmp;
}
示例7: encodeToQrCode
import com.google.zxing.qrcode.QRCodeWriter; //导入方法依赖的package包/类
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;
}
示例8: setupQrcode
import com.google.zxing.qrcode.QRCodeWriter; //导入方法依赖的package包/类
private void setupQrcode() {
QRCodeWriter writer = new QRCodeWriter();
try {
BitMatrix bitMatrix = writer.encode(profile, BarcodeFormat.QR_CODE, 512, 512);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
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, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
((ImageView) findViewById(R.id.qrcode)).setImageBitmap(bmp);
} catch (WriterException e) {
e.printStackTrace();
}
}
示例9: createQrCode
import com.google.zxing.qrcode.QRCodeWriter; //导入方法依赖的package包/类
/**
* Erzeugt aus einem �bergebenen String einen QR Code mit der �bergebenen Gr��e. Wobei diese sich dynamisch verh�lt
* was jedoch bereits aus der ZXing Bibliothek so kommt. Die Fehlerkorrektur betr�gt 25%, dass hei�t bis zu
* 25% der Daten k�nnen wieder hergestellt werden, wenn sie nicht korrekt gelesen werden. Dies ist notwendig
* da Drucken und Scannen verlustbehaftet ist.
*
* @param content
* @param qrCodeSize
* @return
* @throws WriterException
*/
private static BufferedImage createQrCode(String content, int qrCodeSize) throws WriterException {
// Create the ByteMatrix for the QR-Code that encodes the given String.
System.out.println("L�nge: "+content.length());
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
// Make the BufferedImage that are to hold the QRCode
int matrixWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
// Paint and save the image using the ByteMatrix
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++)
{
for (int j = 0; j < matrixWidth; j++)
{
if (byteMatrix.get(i, j) == true)
{
graphics.fillRect(i, j, 1, 1);
}
}
}
return image;
//ImageIO.write(image, imageFormat, outputStream);
}
示例10: generateQRCode
import com.google.zxing.qrcode.QRCodeWriter; //导入方法依赖的package包/类
/**
* Creates QRCode image of size 400X400
* @param dataPath Path to input data
* @throws Exception
*/
public static void generateQRCode(String dataPath) throws Exception
{
String data =read_from_file(dataPath);
QRCodeWriter writer = new QRCodeWriter();
String genqr=QRCode.filePath+"/QRCode.png";
int img_size=400;
BitMatrix bm = writer.encode(data, BarcodeFormat.QR_CODE,img_size,img_size);
Bitmap bmp = Bitmap.createBitmap(img_size,img_size,Bitmap.Config.ARGB_8888);
if (bmp != null)
{
File f=new File(genqr);
if(f.exists())
f.delete();
FileOutputStream gqr=new FileOutputStream(genqr);
for (int i = 0; i < img_size; i++)
for (int j = 0; j < img_size; j++)
bmp.setPixel(i, j, bm.get(i, j) ? Color.BLACK: Color.WHITE);
bmp.compress(Bitmap.CompressFormat.PNG, 100,gqr);
str+="\nQRCode img: "+genqr;
gqr.close();
}
else
throw new WriterException("QRCode generation failed!");
}
示例11: generateQRCode
import com.google.zxing.qrcode.QRCodeWriter; //导入方法依赖的package包/类
public Bitmap generateQRCode() {
QRCodeWriter writer = new QRCodeWriter();
try {
BitMatrix matrix = writer.encode(this.content, BarcodeFormat.QR_CODE, 512, 512);
Bitmap bitmap = Bitmap.createBitmap(matrix.getWidth(), matrix.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bitmap);
tempCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
for (int x = 0; x < matrix.getWidth(); x++) {
for (int y = 0; y < matrix.getHeight(); y++) {
bitmap.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.TRANSPARENT);
}
}
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
示例12: setContentString
import com.google.zxing.qrcode.QRCodeWriter; //导入方法依赖的package包/类
public void setContentString(String content) throws WriterException {
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, getWidth(),
getWidth(), hintMap);
int size = byteMatrix.getWidth();
image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(getBackground());
graphics.fillRect(0, 0, size, size);
graphics.setColor(Color.BLACK);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1);
}
}
}
repaint();
}
示例13: qrcode
import com.google.zxing.qrcode.QRCodeWriter; //导入方法依赖的package包/类
@RequireAnyAuthority
@RequestMapping(value = "/qr", method = RequestMethod.GET)
public void qrcode(HttpServletResponse response,
@AuthenticationPrincipal MongoUserDetails userDetails)
throws WriterException, IOException {
User user = userDetails.getUser(this.mongoDb);
if (user != null && StringUtils.hasText(user.getSecret())) {
response.setContentType("image/png");
String contents = "otpauth://totp/" + user.getEmail() + "?secret="
+ user.getSecret() + "&issuer=" + this.appName;
QRCodeWriter writer = new QRCodeWriter();
BitMatrix matrix = writer.encode(contents, BarcodeFormat.QR_CODE, 200, 200);
MatrixToImageWriter.writeToStream(matrix, "PNG", response.getOutputStream());
response.getOutputStream().flush();
}
}
示例14: createQrCode
import com.google.zxing.qrcode.QRCodeWriter; //导入方法依赖的package包/类
/**
* Erzeugt aus einem �bergebenen String einen QR Code mit der �bergebenen Gr��e. Wobei diese sich dynamisch verh�lt
* was jedoch bereits aus der ZXing Bibliothek so kommt. Die Fehlerkorrektur betr�gt 25%, dass hei�t bis zu
* 25% der Daten k�nnen wieder hergestellt werden, wenn sie nicht korrekt gelesen werden. Dies ist notwendig
* da Drucken und Scannen verlustbehaftet ist.
*
* @param content
* @param qrCodeSize
* @return
* @throws WriterException
*/
private static BufferedImage createQrCode(String content, int qrCodeSize) throws WriterException {
// Create the ByteMatrix for the QR-Code that encodes the given String.
System.out.println("L�nge: "+content.length());
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
// Make the BufferedImage that are to hold the QRCode
int matrixWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
// Paint and save the image using the ByteMatrix
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++)
{
for (int j = 0; j < matrixWidth; j++)
{
if (byteMatrix.get(i, j) == true)
{
graphics.fillRect(i, j, 1, 1);
}
}
}
return image;
//ImageIO.write(image, imageFormat, outputStream);
}
示例15: createQRCode
import com.google.zxing.qrcode.QRCodeWriter; //导入方法依赖的package包/类
/**
* Creates a QR code bitmap with the specified dimensions from the specified string.
*
* @param content The data the QR code should represent.
* @param width The preferred width of the bitmap.
* @param height The preferred height of the bitmap.
* @return The bitmap containing the QR code.
*/
protected Bitmap createQRCode(String content, int width, int height) {
// Create BitMatrix from content string
BitMatrix bitMatrix;
QRCodeWriter writer = new QRCodeWriter();
try {
bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height);
} catch (WriterException e) {
throw new RuntimeException(e);
}
// Create bitmap from BitMatrix
width = bitMatrix.getWidth();
height = bitMatrix.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
bitmap.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE);
}
}
return bitmap;
}