本文介紹了在矢量圖像中創(chuàng)建二維碼的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我可以用ZXing成功創(chuàng)建二維碼PNG圖像,但沒(méi)有簡(jiǎn)單的方法可以獲得SVG或EPS格式的輸出。
如何從QRCodeWriter創(chuàng)建的BitMatrix對(duì)象創(chuàng)建矢量圖像?
我發(fā)現(xiàn)最簡(jiǎn)單的方法是用iText創(chuàng)建推薦答案,然后將生成的pdf轉(zhuǎn)換為eps或svg。以下是創(chuàng)建PDF的代碼:
@Test
public void testQRtoPDF() throws WriterException, FileNotFoundException, DocumentException, UnsupportedEncodingException {
final int s = 600;
int r = 1;
Charset charset = Charset.forName( "UTF-8" );
CharsetEncoder encoder = charset.newEncoder();
byte[] b = null;
try {
// Convert a string to UTF-8 bytes in a ByteBuffer
ByteBuffer bbuf = encoder.encode( CharBuffer.wrap(
"1é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò1" +
"2é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò2" +
"3é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò3" +
"4é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò4" +
"5é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò5" +
"6é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò??é?à?èüùò6" ) );
b = bbuf.array();
} catch ( CharacterCodingException e ) {
System.out.println( e.getMessage() );
}
String content = new String( b, "UTF-8" );
QRCodeWriter qrCodeWriter = new QRCodeWriter();
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>( 2 );
hints.put( EncodeHintType.CHARACTER_SET, "UTF-8" );
BitMatrix qrCode = qrCodeWriter.encode( content, BarcodeFormat.QR_CODE, s, s, hints );
Document doc = new Document( new Rectangle( s, s ) );
PdfWriter pdfWriter = PdfWriter.getInstance( doc, new FileOutputStream( "qr-code.pdf" ) );
doc.open();
PdfContentByte contentByte = pdfWriter.getDirectContent();
contentByte.setColorFill( BaseColor.BLACK );
boolean d = false;
for ( int x = 0; x < qrCode.getWidth(); x += r ) {
for ( int y = 0; y < qrCode.getHeight(); y += r ) {
if ( qrCode.get( x, y ) ) {
contentByte.rectangle( x, s - y, r, r );
contentByte.fill();
contentByte.stroke();
}
}
}
doc.close();
}
然后我使用圖像魔術(shù)進(jìn)行轉(zhuǎn)換。如下所示:
convert qr-code.pdf qr-code.eps
不能對(duì)SVG執(zhí)行同樣的操作
convert qr-code.pdf qr-code.svg
這不起作用
我用一些長(zhǎng)內(nèi)容測(cè)試了這段代碼,它可以處理多達(dá)600個(gè)字符。這可能取決于手機(jī)或屏幕上攝像頭的精確度。
我希望這能幫助某人
這篇關(guān)于在矢量圖像中創(chuàng)建二維碼的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,