01.
public static void main(String[] args) {
02.
Charset charset = Charset.forName(
"ISO-8859-1"
);
03.
CharsetEncoder encoder = charset.newEncoder();
04.
byte[] b =
null
;
05.
try
{
06.
07.
ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(
"hello world"
));
08.
b = bbuf.array();
09.
}
catch
(CharacterCodingException e) {
10.
System.out.println(e.getMessage());
11.
}
12.
13.
String data;
14.
try
{
15.
data =
new
String(b,
"ISO-8859-1"
);
16.
}
catch
(UnsupportedEncodingException e) {
17.
System.out.println(e.getMessage());
18.
}
19.
20.
21.
BitMatrix matrix =
null
;
22.
int h = 100;
23.
int w = 100;
24.
com.google.zxing.Writer writer =
new
QRCodeWriter();
25.
try
{
26.
matrix = writer.encode(data,
27.
com.google.zxing.BarcodeFormat.QR_CODE, w, h);
28.
}
catch
(com.google.zxing.WriterException e) {
29.
System.out.println(e.getMessage());
30.
}
31.
32.
String filePath =
"C:\\temp\\qr_png.png"
;
33.
File file =
new
File(filePath);
34.
try
{
35.
MatrixToImageWriter.writeToFile(matrix,
"PNG"
, file);
36.
System.out.println(
"printing to "
+ file.getAbsolutePath());
37.
}
catch
(IOException e) {
38.
System.out.println(e.getMessage());
39.
}
40.
}