Verification code picture to string

Source: Internet
Author: User

Why?

Because the previous project, Android said only to deal with JSON, nothing else ... (later asked others, obviously can handle other ~ ~)

At that time, because of the progress, so directly cached the picture (embarrassed), and then send the image address.

After thinking about the next completely can be turned into a string sent past.

Here's how:

Verification Code Picture

 PublicBufferedImage GetImage ()throwsioexception{intwidth = 60; intHeight = 32; //Create the imageBufferedImage image =Newbufferedimage (width, height, bufferedimage.type_int_rgb); Graphics g=Image.getgraphics (); //set the background colorG.setcolor (NewColor (0xDCDCDC)); G.fillrect (0, 0, width, height); //Draw the BorderG.setcolor (Color.Black); G.drawrect (0, 0, width-1, height-1); //Create a random instance to generate the CodesRandom RDM =NewRandom (); String Hash1=integer.tohexstring (Rdm.nextint ()); //Make some confusion     for(inti = 0; I < 50; i++){        intx =rdm.nextint (width); inty =rdm.nextint (height); G.drawoval (x, Y,0, 0); }    //generate a random codeString capstr = hash1.substring (0, 4); G.setcolor (NewColor (0, 100, 0)); G.setfont (NewFont ("Candara", Font.Bold, 24)); g.DrawString (Capstr,8, 24);    G.dispose (); returnimage; }

Direct output in JSP

Tool methods for turning into strings

 Public Static byte[] Object2bytearray (Object object) {Bytearrayoutputstream Bytearrayoutputstream=NewBytearrayoutputstream (); ObjectOutputStream ObjectOutputStream=NULL; byte[] bytes =NULL; Try{ObjectOutputStream=NewObjectOutputStream (Bytearrayoutputstream);        Objectoutputstream.writeobject (object); Bytes=Bytearrayoutputstream.tobytearray ();        Bytearrayoutputstream.close ();        Objectoutputstream.close (); returnbytes; } Catch(IOException e) {e.printstacktrace (); }    return NULL;}

Test method

@Test  Public void throws ioexception{    byte[] bytes = object2bytearray (getImage ());     = Base64.getencoder (). encodetostring (bytes);    System.out.println (str);}

But, does not work!!! Because BufferedImage does not implement a serialized interface, it's a great embarrassment.

Solutions

/*** This class, purely for implementation of the serialization interface! */ Public class  mybufferedimage extendsBufferedImageImplements  Serializable { PublicMybufferedimage (intWidthintHeightintImageType) {        Super(width, height, imageType); }     PublicMybufferedimage (intWidthintHeightintImageType, IndexColorModel cm) {        Super(width, height, imageType, cm); }     PublicMybufferedimage (ColorModel cm, writableraster raster,BooleanIsrasterpremultiplied, hashtable<?,? >properties) {        Super(cm, Raster, israsterpremultiplied, properties); }}

And then

 Public  mybufferedimage GetImage ()throwsioexception{intwidth = 60; intHeight = 32; //Create the image     mybufferedimage Image =New mybufferedimage(width, height, bufferedimage.type_int_rgb); Graphics g=Image.getgraphics (); //set the background colorG.setcolor (NewColor (0xDCDCDC)); G.fillrect (0, 0, width, height); //Draw the BorderG.setcolor (Color.Black); G.drawrect (0, 0, width-1, height-1); //Create a random instance to generate the CodesRandom RDM =NewRandom (); String Hash1=integer.tohexstring (Rdm.nextint ()); //Make some confusion     for(inti = 0; I < 50; i++){        intx =rdm.nextint (width); inty =rdm.nextint (height); G.drawoval (x, Y,0, 0); }    //generate a random codeString capstr = hash1.substring (0, 4); G.setcolor (NewColor (0, 100, 0)); G.setfont (NewFont ("Candara", Font.Bold, 24)); g.DrawString (Capstr,8, 24);    G.dispose (); returnimage;}

Now it's OK to run the test again.

Verification code picture to string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.