我用我的development.When我創建了一個簡單的servlet,其用於生成驗證碼最新的JDK 7u45,我發現圖像是相當blurred.The圖像看起來就像這樣:Creatting配備Retina支持一個BufferedImage
的圖像質量是比較差的,而且看起來真的很差,我的MacBook Pro的Retina顯示屏。 我使用創建的BufferedImage的代碼很簡單:
public static BufferedImage getImage() {
BufferedImage image = new BufferedImage(IMAGE_WIDTH,IMAGE_HEIGHT,BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
drawRandomNumber((Graphics2D)graphics);
return image;
}
private static void drawRandomNumber(Graphics2D graphics) {
graphics.setColor(Color.RED);
graphics.setFont(new Font("Arial",Font.BOLD|Font.ITALIC,RANDOM_NUMBER_FONT_SIZE));
char lch = 'a',hch = 'z';
char lcach = 'A',hcach = 'Z';
int x = RANDOM_NUMBER_START_X;
for (int i = 0;i < RANDOM_NUMBER_NUM;i++) {
char ch = new Random().nextBoolean() ?
(char)(new Random().nextInt(hch - lch) + lch) :
(char)(new Random().nextInt(hcach - lcach) + lcach);
int degree = new Random().nextInt() % 30;
graphics.rotate(degree * Math.PI/180,x,RANDOM_NUMBER_START_Y);
graphics.drawString(ch + "",x,RANDOM_NUMBER_START_Y);
graphics.rotate(-degree * Math.PI/180,x,RANDOM_NUMBER_START_Y);
x += 60;
}
}
而且該圖像寫入到servlet代碼:
ImageIO.write(image, "png", response.getOutputStream());
我只是想知道如果我想創建一個支持HDPI顯示的高質量圖像,我該怎麼辦?有沒有什麼方法可以用java代碼生成高質量的圖像?我嘗試了很多方法,但它們似乎毫無幫助。
你不應該像你在你的代碼中那樣使用Random類。將發生器存儲在一個變量中,以便重新使用它。此外,最好調用'nextInt(30)'而不是進行模運算。 http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextInt(int) –