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