2012-11-18 99 views
2

enter image description here路徑背景中的servlet

//下面線是我的HTML形式的這使得通過下面進一步寫入該servlet發送的圖像的元素。

<img style="margin-left:91px; margin-top:-6px;" class="image" src="http://www.abcd.com/captchaServlet"> 

我使用java中的以下代碼生成驗證碼。

public class captchaServlet extends HttpServlet { 


    protected void processRequest(HttpServletRequest request, 
           HttpServletResponse response) 
       throws ServletException, IOException { 

    int width = 150; 
    int height = 50; 
    int charsToPrint = 6; 

    String elegibleChars = "ABCDEFGHJKLMPQRSTUVWXYabcdefhjkmnpqrstuvwxy1234567890"; 

    char[] chars = elegibleChars.toCharArray(); 
StringBuffer finalString = new StringBuffer(); 

    for (int i = 0; i < charsToPrint; i++) { 
    double randomValue = Math.random(); 
    int randomIndex = (int) Math.round(randomValue * (chars.length - 1)); 
    char characterToShow = chars[randomIndex]; 
    finalString.append(characterToShow); 
    } 
    System.out.println(finalString); 

    BufferedImage bufferedImage = new BufferedImage(width, height, 
        BufferedImage.TYPE_INT_RGB); 

    Graphics2D g2d = bufferedImage.createGraphics(); 

    Font font = new Font("Georgia", Font.BOLD, 18); 
    g2d.setFont(font); 

    RenderingHints rh = new RenderingHints(
      RenderingHints.KEY_ANTIALIASING, 
      RenderingHints.VALUE_ANTIALIAS_ON); 

    rh.put(RenderingHints.KEY_RENDERING, 
      RenderingHints.VALUE_RENDER_QUALITY); 

    g2d.setRenderingHints(rh); 

    GradientPaint gp = new GradientPaint(0, 0, 
    Color.BLUE, 0, height/2, Color.black, true); 

    g2d.setPaint(gp); 
    g2d.fillRect(0, 0, width, height); 

    g2d.setColor(new Color(255, 255, 0)); 

    Random r = new Random(); 
    int index = Math.abs(r.nextInt()) % 5; 
    char[] data=new String(finalString).toCharArray(); 
    String captcha = String.copyValueOf(data); 
    int x = 0; 
    int y = 0; 

    for (int i=0; i<data.length; i++) { 
     x += 10 + (Math.abs(r.nextInt()) % 15); 
     y = 20 + Math.abs(r.nextInt()) % 20; 
     g2d.drawChars(data, i, 1, x, y); 
    } 

    g2d.dispose(); 

    response.setContentType("image/png"); 
    OutputStream os = response.getOutputStream(); 
    ImageIO.write(bufferedImage, "png", os); 
    os.close(); 
    } 


    protected void doGet(HttpServletRequest request, 
         HttpServletResponse response) 
          throws ServletException, IOException { 
     processRequest(request, response); 
    } 


    protected void doPost(HttpServletRequest request, 
         HttpServletResponse response) 
          throws ServletException, IOException { 
     processRequest(request, response); 
    } 
} 

但在上面的代碼背景也是使用我猜測的setPaint menthod生成的。我希望背景是從我的本地機器whoz URL一些像我應該能夠提像

URL url=this.getClass().getResource("Desktop/images.jpg"); 
BufferedImage bufferedImage = ImageIO.read(url); 

我只是寫了上面兩行爲了使讀者更好地瞭解是什麼問題。不想使用完全相同的命令。我想要的是生成的驗證碼的背景應該是我選擇的圖像。

+0

爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)的你代碼到目前爲止。熱鏈接到BG圖像(例如來自[此處](http://pscode.org/media/#image))或用代碼生成一個。 –

+0

@AndrewThompson嗨,我已經上傳了一個正在運行的代碼,並且還顯示了一個圖像,顯示瞭如何生成驗證碼。所以你看到圖像中的藍色和黑色背景,我想用我自己的圖像替換它。 –

+0

對於servlet,請看[[ServletContext.getResourceAsStream(String)'](http://docs.oracle.com/javaee/5/api/javax/servlet/ServletContext.html#getResourceAsStream%28java.lang.String %29)。 –

回答

2

與其說

g2d.setPaint(gp); 
g2d.fillRect(0, 0, width, height); 

的你可能需要調用是這樣的:

g2d.drawImage(bufferedImage, 0, 0, null); 
+0

lemme試試吧! ............. –

+0

這是我的錯誤,代碼有效。謝謝。 –