zoukankan      html  css  js  c++  java
  • 验证码图片转字符串

    为什么?

    因为之前一个项目,安卓那边说只能处理JSON,别的都不行。。。(后来问过他人,明明可以处理其他的~~)

    当时因为赶进度,所以直接缓存了图片(囧),然后将图片地址发出去。

    过后想了下完全可以转成字符串发送过去。

    方法如下:

    验证码图片

    public BufferedImage getImage() throws IOException{
        int width = 60;
        int height = 32;
        //create the image
        BufferedImage image =  new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
    
        // set the background color
        g.setColor(new Color(0xDCDCDC));
        g.fillRect(0, 0, width, height);
        // draw the border
        g.setColor(Color.black);
        g.drawRect(0, 0, width - 1, height - 1);
        // create a random instance to generate the codes
        Random rdm = new Random();
        String hash1 = Integer.toHexString(rdm.nextInt());
        // make some confusion
        for(int i = 0; i < 50; i++){
            int x = rdm.nextInt(width);
            int y = rdm.nextInt(height);
            g.drawOval(x, y, 0, 0);
        }
        // generate a random code
        String capstr = hash1.substring(0, 4);
    
        g.setColor(new Color(0, 100, 0));
        g.setFont(new Font("Candara", Font.BOLD, 24));
        g.drawString(capstr, 8, 24);
        g.dispose();
    
        return image;
    }

    JSP中直接输出

    ImageIO.write(image, "JPEG", response.getOutputStream()); 

    转成字符串的工具方法

    public static byte[] object2byteArray(Object object){
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = null;
        byte[] bytes = null;
    
        try{
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(object);
            bytes = byteArrayOutputStream.toByteArray();
    
            byteArrayOutputStream.close();
            objectOutputStream.close();
            return bytes;
        } catch(IOException e){
            e.printStackTrace();
        }
        return null;
    }

    测试方法

    @Test
    public void run() throws IOException{
        byte[] bytes = object2byteArray(getImage());
        String str = Base64.getEncoder().encodeToString(bytes);
        System.out.println(str);
    }

    但是,行不通!!!因为BufferedImage没有实现序列化接口,囧囧有神~

    解决办法

    /**
     * 这个类,纯粹为了实现序列化接口!
     */
    public class MyBufferedImage extends BufferedImage implements Serializable {
        public MyBufferedImage(int width, int height, int imageType){
            super(width, height, imageType);
        }
    
        public MyBufferedImage(int width, int height, int imageType, IndexColorModel cm){
            super(width, height, imageType, cm);
        }
    
        public MyBufferedImage(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable<?, ?> properties){
            super(cm, raster, isRasterPremultiplied, properties);
        }
    }

    然后

    public MyBufferedImage getImage() throws IOException{
        int width = 60;
        int height = 32;
        //create the image
        MyBufferedImage image =  new MyBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
    
        // set the background color
        g.setColor(new Color(0xDCDCDC));
        g.fillRect(0, 0, width, height);
        // draw the border
        g.setColor(Color.black);
        g.drawRect(0, 0, width - 1, height - 1);
        // create a random instance to generate the codes
        Random rdm = new Random();
        String hash1 = Integer.toHexString(rdm.nextInt());
        // make some confusion
        for(int i = 0; i < 50; i++){
            int x = rdm.nextInt(width);
            int y = rdm.nextInt(height);
            g.drawOval(x, y, 0, 0);
        }
        // generate a random code
        String capstr = hash1.substring(0, 4);
    
        g.setColor(new Color(0, 100, 0));
        g.setFont(new Font("Candara", Font.BOLD, 24));
        g.drawString(capstr, 8, 24);
        g.dispose();
    
        return image;
    }

    现在再执行测试就OK啦。

  • 相关阅读:
    福大软工 · 第十次作业
    最终作业:软工实践个人总结
    第三视角Beta答辩总结
    Beta 冲刺(7/7)
    Beta 冲刺 (6/7)
    Beta 冲刺 (5/7)
    Beta 冲刺 (4/7)
    Beta 冲刺 (3/7)
    Beta 冲刺 (2/7)
    福大软工 · 第十次作业
  • 原文地址:https://www.cnblogs.com/larryzeal/p/5570665.html
Copyright © 2011-2022 走看看