Java Forum / GUI / June 2004
Hold jpg as a Java-variable
Erik Sundlo - 04 Jun 2004 17:10 GMT Hi,
I have a Java-application that I've made into an applet. My initial problem is to reproduce the print-functionality the original application had. I get 'no print service' when calling 'java.awt.print.PrinterJob.getPrintService()' (security reasons I suppose).
The stuff to be printed is stored as a BufferedImage. I would like to transform this into a public Java-variable containing a jpg-image. In this way I can access the variable from a JavaScript in the browser (JavaScript can access public variables from the applet), display it and let the user print it from the browser.
This code write the bufferedImage to a file:
BufferedImage printImage = area.printStuff(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageOutputStream ios = ImageIO.createImageOutputStream(bos); ImageIO.write(printImage, "JPEG", ios);
I would like the last sentence to somehow transform the BufferedImage to a jpg-variable. Is this possible ? Using Java SDK 1.4.2.
Regards Erik
Andrew Thompson - 04 Jun 2004 18:37 GMT > I have a Java-application that I've made into an applet. My initial problem > is to reproduce > the print-functionality the original application had. I get 'no print > service' when calling > 'java.awt.print.PrinterJob.getPrintService()' (security reasons I suppose). Stop guessing and read the 'applet security' part of the Sun Java tutorials.
Find your console while you are at it.
(image)
> ...display it and let the > user print it from the browser. > > This code write the bufferedImage to a file: File. Forget it. Read the applet security tutorial, then learn how to jar and sign your code. <http://www.physci.org/codes/javafaq.jsp#jar> Then you can install it with JWS. <http://www.physci.org/codes/javafaq.jsp#jws>
 Signature Andrew Thompson http://www.PhySci.org/ Open-source software suite http://www.PhySci.org/codes/ Web & IT Help http://www.1point1C.org/ Science & Technology
Erik Sundlo - 04 Jun 2004 19:49 GMT > Stop guessing and read the 'applet security' I wasn't really guessing. I know it won't work. I agree that the phrase 'I suppose' is a bit unscientific.
> File. Forget it. Read the applet security > tutorial, then learn how to jar and sign your code. It is a jar-file, and it has to be run as an applet within a web-page. I don't wan't files, quote myself: " ImageIO.write(printImage, "JPEG", ios);
I would like the last sentence to somehow transform the BufferedImage to a jpg-variable. Is this possible ? "
And I'm running JWS. I can't really see that you have helped me with my problem.
Erik
Laird Nelson - 04 Jun 2004 19:55 GMT > And I'm running JWS. I can't really see that you have helped me with my > problem. But beware; he'll outlast you in this thread.
Cheers, Laird
ak - 04 Jun 2004 21:23 GMT > And I'm running JWS. I can't really see that you have helped me with my > problem. He helped you!
He said "read about applet security" - very useful hint for your problem. He said "don't guess - look at java console" - you will find there stack trace He said "you need signed applet" - with signed applet you can do such things. He said "Forget it" - if you can't print, you also can't save your image as jpeg
Regards
ak
 Signature http://uio.dev.java.net http://reader.imagero.com
Erik Sundlo - 04 Jun 2004 22:56 GMT > He said "read about applet security" - very useful hint for your problem. > He said "don't guess - look at java console" - you will find there stack [quoted text clipped - 3 lines] > He said "Forget it" - if you can't print, you also can't save your image as > jpeg I want to store a jpg-image in a Java memory variable, and then access the variable from a JavaScript. My point is: can this be done _without_ any signing and breaking of security ? As long as public variables can be accessed from outside (i.e. a JavaScript), I thought maybe this could be done in some way. I don't know why you guys suddenly starts off nagging about security and signing, it should be a simple question. I've never said anything about saving to any damn files !
Erik
ak - 04 Jun 2004 23:26 GMT > I want to store a jpg-image in a Java memory variable, and then access the > variable from a JavaScript. My point is: can this be done _without_ any > signing and breaking of security ? As long as public variables can be > accessed from outside (i.e. a JavaScript), I thought maybe this could be > done in some way. no, no, no and no.
 Signature http://uio.dev.java.net http://reader.imagero.com
Erik Sundlo - 05 Jun 2004 00:27 GMT > > I want to store a jpg-image in a Java memory variable, and then access the > > variable from a JavaScript. My point is: can this be done _without_ any > > signing and breaking of security ? As long as public variables can be > > accessed from outside (i.e. a JavaScript), I thought maybe this could be > > done in some way. > no, no, no and no. There you go. Thanks.
But -- a Java-variable could contain anything; text, numbers or even a bunch of bytes representing a jpg. That's really what I was asking: transform a BufferedImage into jpg-data, write the jpg-data to memory and let that piece of memory be represented as a public Java variable. You're not allowed to write to files, but you're allowed to write to the applets allocated memory. Again, the line
ImageIO.write(printImage, "JPEG", ios);
should somehow be printing to an adress in memory instead of a file. And there, and nowhere else, lies the challenge !
Erik
Erik Sundlo - 05 Jun 2004 00:33 GMT >You're not allowed to write to files, but you're allowed to write to the applets allocated memory.
Ehm. Is this correct ? You can create variables in a controlled fashion, but 'writing to memory' is another thing.OK, maybe I'm on the wrong path jere...
Erik
Rogan Dawes - 07 Jun 2004 15:32 GMT > Hi, > [quoted text clipped - 25 lines] > Regards > Erik How about something like:
public String getImage() { BufferedImage printImage = area.printStuff(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageOutputStream ios = ImageIO.createImageOutputStream(bos); ImageIO.write(printImage, "JPEG", ios); ios.close(); return new String(bos.toByteArray()); }
in your class, and
in your HTML, <img src="javascript:applet.getImage()"></img>
IIRC, this should work, though I have never tried it. You may have to set a locale, or character set, or something similar.
Here is a page that describes this:
http://developer.irt.org/script/955.htm
You basically end up doing:
<html><head></head> <body> <script language="JavaScript"><!-- gif = 'GIF87a \0\20\0\200\0\0\0\0\0\377\377\377,\0\0\0\0 \0\20'+ '\0\0\2 \214\217\251\313\355\17\243D\240\326\11\1\236zG\333y\231'+ '8\222J\30\232\7\252\236W\13\307\362\274\25\0;'; //--></script>
<img name="myImage" src="javascript:gif" width="32" height="32"> </body> </html>
Granted, the example given uses GIF, and you are asking for JPEG, but it might still work.
Regards,
Rogan
 Signature Rogan Dawes
*ALL* messages to discard@dawes.za.net will be dropped, and added to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"
Erik Sundlo - 08 Jun 2004 15:41 GMT > How about something like: > [quoted text clipped - 5 lines] > ios.close(); > return new String(bos.toByteArray()); Thanks for your answer, I started to think I was way out of line.
I followed your instructions, but couldn't get it to work. I decided to find out exactly what 'bos.toByteArray()' contained and wrote it out to a file in a standalone Java-application. I also printed the image itself, see code below:
// Print jpg to file ImageIO.write(printImage, "JPEG", new File("C:/temp/file.jpg"));
// Print jpg-string to file ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageOutputStream ios = ImageIO.createImageOutputStream(bos); ImageIO.write(printImage, "JPEG", ios); ios.close(); strPrintImage = new String(bos.toByteArray()); FileOutputStream fout = new FileOutputStream ("C:/temp/string.jpg"); new PrintStream(fout).print (strPrintImage); fout.close();
The two files 'file.jpg' and 'string.jpg' have the exact same size, but the 'string.jpg' looks like garbage when i view it, whereas 'file.jpg' looks ok. I ran a binary diff on both of them and found that the 'string.jpg' has swapped chars with decimal-values 129 and 143 (oct 81 and 8F) to 63 (oct 3F). Thought it might have something to do with 7bit/8it, but other decimal-values above 128 have not been swapped.
I will try to work some more on this, but of course any hints would be nice...
Regards Erik
JR - 08 Jun 2004 20:26 GMT Hex 3F is ? (question mark). What is your default encoding (System.getProperty("file.encoding"))? 81 and 8f are probably undefined.
Jony
> > How about something like: > > [quoted text clipped - 38 lines] > Regards > Erik Erik Sundlo - 08 Jun 2004 21:39 GMT > Hex 3F is ? (question mark). What is your default encoding > (System.getProperty("file.encoding"))? 81 and 8f are probably undefined. Getting closer ! However, I still can't figure it out.
My "file.encoding" was Cp1252. I then tried different parameters to the line
strPrintImage = new String(bos.toByteArray(), "ISO8859_1");
In addition to "ISO8859_1" I've tried "UTF8", "UTF16", "ASCII", "UnicodeBig" and "Windows-1252". They have an effect, because the file differs in size and content, but there still some chars it can't write properly. "Cp1252" and "ISO8829_1" seemed the closest, though. Is there a way to define 81 and 8f ?
Regards Erik
JR - 09 Jun 2004 06:14 GMT For binary data, you should use a byte file, which avoids the encoding process.
Jony
> > Hex 3F is ? (question mark). What is your default encoding > > (System.getProperty("file.encoding"))? 81 and 8f are probably undefined. [quoted text clipped - 13 lines] > Regards > Erik Erik Sundlo - 10 Jun 2004 08:37 GMT > For binary data, you should use a byte file, which avoids the encoding > process. Ahm. Seems I'm stuck. Can it be done -- convert to a string that contain binary data ?
Erik
Roedy Green - 10 Jun 2004 09:25 GMT >Ahm. Seems I'm stuck. Can it be done -- convert to a string that contain >binary data ? see http://mindprod.com/jgloss/armouring.html
 Signature Canadian Mind Products, Roedy Green. Coaching, problem solving, economical contract programming. See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
JR - 10 Jun 2004 16:42 GMT You have binary data, but you are converting it to String. If you just want to see it, print it as a sequence of hexadecimal values, two characters per byte. Of look at the file with a viewer that shows hex.
Jony
> > For binary data, you should use a byte file, which avoids the encoding > > process. [quoted text clipped - 3 lines] > > Erik
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|