Created
May 15, 2014 23:37
-
-
Save dsdstudio/4555f0e64243bb88e8e7 to your computer and use it in GitHub Desktop.
QRCode generation prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.zxing.common.BitMatrix; | |
import com.google.zxing.qrcode.QRCodeWriter; | |
import javax.imageio.ImageIO; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.TimeUnit; | |
import static com.google.zxing.BarcodeFormat.QR_CODE; | |
/** | |
* QRCode Test | |
* | |
* @author : bhkim | |
* @since : 2014-04-28 오후 3:27 | |
*/ | |
public class Main { | |
private static final Path BaseImagePath = Paths.get("/Users/bhkim/temp"); | |
static ExecutorService q = Executors.newFixedThreadPool(4); | |
static class Job implements Runnable { | |
private int start; | |
private int end; | |
public Job(int start, int end) { | |
this.start = start; | |
this.end = end; | |
} | |
@Override | |
public void run() { | |
for (; start < end; start++) | |
writeQRImage("http://memestudio.kr/code?q=cXdlcnF3ZXJxd2VycWVyYWZkYXNkZmFzZGZhc2RmYXNkZmFzZGZhc2RmcXdlcnF3ZXJxd2VycWVy\n" + | |
"YWZkYXNkZmFzZGZhc2RmYXNkZmFzZGZhc2RmcXdlcnF3ZXJxd2VycWVyYWZkYXNkZmFzZGZhc2Rm" + | |
"YXNkZmFzZGZhc2RmcXdlcnF3ZXJxd2VycWVyYWZkYXNkZmFzZGZhc2RmYXNkZmFzZGZhc2Rm", start); | |
} | |
} | |
public static void main(String[] args) throws InterruptedException { | |
int cpucount = Runtime.getRuntime().availableProcessors(); | |
int jobCount = 10000; // 좝 카운트 | |
int div = jobCount / cpucount; // 좝당 할당할 작업 갯수 | |
int jobOffset = 0; | |
System.out.println("Processor number: " + cpucount); | |
for (int i = 0; i < cpucount; i++) { | |
q.submit(new Job(jobOffset, jobOffset + div)); | |
jobOffset += div; | |
} | |
q.shutdown(); | |
q.awaitTermination(1, TimeUnit.DAYS); | |
} | |
public static void writeQRImage(String content, int i) { | |
int imageSize = 256; | |
QRCodeWriter writer = new QRCodeWriter(); | |
try { | |
BitMatrix matrix = writer.encode(content, QR_CODE, imageSize, imageSize, null); | |
BufferedImage img = toImage(matrix); | |
ImageIO.write(img, "png", BaseImagePath.resolve("a" + i + ".png").toFile()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* QRCode Matrix 를 Image 형태로 변환한다. | |
* | |
* @param mat | |
* @return BufferedImage | |
*/ | |
static BufferedImage toImage(BitMatrix mat) { | |
int width = mat.getWidth(), height = mat.getHeight(); | |
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); | |
for (int x = 0; x < width; x++) { | |
for (int y = 0; y < height; y++) { | |
image.setRGB(x, y, (mat.get(x, y) ? Color.BLACK : Color.WHITE).getRGB()); | |
} | |
} | |
return image; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment