maven project
1. dependencies at pom.xml
... ... ... <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>13.0.1</version> <type>jar</type> </dependency> <dependency> <groupId>net.glxn</groupId> <artifactId>qrgen</artifactId> <version>1.1</version> </dependency> </dependencies> <repositories> <repository> <id>QRGen</id> <name>QRGen Repo</name> <url>http://kenglxn.github.com/QRGen/repository</url> </repository> </repositories> ... ... ...2. write code
package com.blogspot.na5cent.learning.qrcode; import com.google.common.io.Files; import com.google.common.io.InputSupplier; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.logging.Level; import java.util.logging.Logger; import net.glxn.qrgen.QRCode; //**** /** * @author redcrow * */ public class Main { private static final Logger LOG = Logger.getLogger(Main.class.getName()); private static final File parent = new File("D:\\learning\\java\\QRCode\\"); static { if (!parent.exists()) { parent.mkdirs(); } } public static void main(String[] args) { String clearText = "com.blogspot.na5cent"; String outputFileName = "QRCode.png"; InputStream inputStream = null; try { File file = QRCode.from(clearText).file(); //***** inputStream = new FileInputStream(file); copyFile(parent, outputFileName, inputStream); } catch (FileNotFoundException ex) { LOG.log(Level.SEVERE, null, ex); } catch (IOException ex) { LOG.log(Level.SEVERE, null, ex); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException ex) { LOG.log(Level.SEVERE, null, ex); } } } } private static void copyFile(File pathfile, String fileName, final InputStream inputStream) throws IOException { Files.copy(new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return inputStream; } }, new File(pathfile, fileName)); } }
ไม่มีความคิดเห็น:
แสดงความคิดเห็น