image - Java display base 64 byte array in jsp -



image - Java display base 64 byte array in jsp -

i have class called graphic creates new bufferedimage, draws new graphics2d , returns image base64 encoded string:

bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_argb); graphics2d g2 = image.creategraphics(); g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); // draw background g2.setcolor(color.decode("#ffffff")); g2.fillrect(0, 0, grafikbreite, grafikhoehe); g2.setcolor(color.decode("#000000")); // draw rectangles , other stuff... drawstuff(g2); bytearrayoutputstream baos = new bytearrayoutputstream(); byte[] imageinbyte = {}; seek { jpegimageencoder j = new jpegimageencoderimpl(baos); j.encode(image); imageinbyte = baos.tobytearray(); baos.close(); } grab (ioexception e) { e.printstacktrace(); } homecoming javax.xml.bind.datatypeconverter.printbase64binary(imageinbyte);

in jsp-file want display image using, graphic created base64 byte array:

<img src="data:image/jpg;base64,<c:out value="${graphic}"/>"/>

the image displayed, problem image has reddish background , other colors used wrong. if save created base64 string jpeg-file on hard disk, colors displayed correctly.

has thought why html displays image unusual colors?

thanks help

first bit cleaning up:

g2.setcolor(color.white); g2.fillrect(0, 0, grafikbreite, grafikhoehe); g2.setcolor(color.black); drawstuff(g2); g2.dispose(); // todo

dispose after creategraphics.

then 1 seek more generic, portable imageio class. parametrising antialiasing , such goes bit different, jpeg lossy format anyway. seek different angle.

imageio.write(image, "jpg", baos); baos.close(); imageinbyte = baos.tobytearray();

and did closing first. (it has no effect javadoc.)

one seek .png , type, abgr.

i think imageio trick, or code abgr.

java image jsp base64 bufferedimage

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -