1) You need to get the byte code of the image and save it.

2) The resulting byte-code is inserted into the program code "as a string".

3) From the recorded byte-code to create an image.

The problem is that it is impossible to save byte code (if you write to txt, it is converted and it can no longer be used).

package image; 

import java.awt.image.BufferedImage; import java.io. *; import javax.imageio.ImageIO;

public class ImageTest {

 private static OutputStream txt; public static void main(String[] args) { try { byte[] imageInByte; BufferedImage originalImage = ImageIO.read(new File("e:/darksouls.jpg")); // конвСртация изобраТСния Π² массив Π±Π°ΠΉΡ‚ΠΎΠ² ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(originalImage, "jpg", baos); baos.flush(); imageInByte = baos.toByteArray(); baos.close(); // конвСртация массива Π±Π°ΠΉΡ‚ΠΎΠ² Π² ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ InputStream in = new ByteArrayInputStream(imageInByte); BufferedImage bImageFromConvert = ImageIO.read(in); txt = new FileOutputStream("e:/dark.svg"); //запись массива Π±Π°ΠΉΡ‚ΠΎΠ² Π² Ρ„Π°ΠΉΠ»1 txt.write(imageInByte); ImageIO.write(bImageFromConvert, "jpg", new File("e:/new-darksouls.svg"));//запись массива Π±Π°ΠΉΡ‚ΠΎΠ² Π² Ρ„Π°ΠΉΠ»2 } catch (IOException e) { System.out.println(e.getMessage()); } } 

} This program converts an image into an array of bytes, and creates a new image from this array.

How to save this array of bytes to create images using a single bytecode (without referring to the original image)?

  • Why do you do so much extra movements and transcoding? You can work with the source file e:/darksouls.jpg as with a set of bytes. You do not need to load an image and then save it to get a set of bytes - you just need to read the set of bytes from the file. - Pavel Mayorov

2 answers 2

2) The resulting byte-code is inserted into the program code "as a string". It is not possible that, in fact, the String in java is a char [] set stored in a specific encoding (usually unicode characters are stored in char, which is what it was designed for). PS The picture is a set of bytes (well, let the char), which are simply corrupted when converted to Unicode.

And it is not clear what you want to do?

 byte[] imageInByte; -> Π½Π΅ΠΉΠΊΠΈΠΉ Π½Π°Π±ΠΎΡ€ Π±Π°ΠΉΡ‚ΠΎΠ² BufferedImage originalImage = ImageIO.read(new File("e:/darksouls.jpg")); -> Π½Ρƒ допустим // convert BufferedImage to byte array ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Π΄ΠΈΠ½Π°ΠΌΠΈΡ‡Π½Ρ‹ΠΉ Π½Π°Π±ΠΎΡ€ Π±Π°ΠΉΡ‚ΠΎΠ², ΠΎΠΊ ImageIO.write(originalImage, "jpg", baos); //? baos.flush(); //? ΠΎΡ‡ΠΈΡΡ‚ΠΈΡ‚ΡŒ? Π·Π°Ρ‡Π΅ΠΌ Ρ‚ΠΎΠ³Π΄Π° заполняли Π΅Π³ΠΎ Ρ‡Π΅-Ρ‚ΠΎ Π½Π΅ Ρ‚ΠΎ Π°? 

"using one byte code" ??

1. Save the image? in the file, having a set of image bytes, ok

 FileOutputStream file = new FileOutputStream("Π±ΡƒΠΊΠ²Π° c Ρ‡Π΅-Ρ‚ΠΎ Ρ‚Π°ΠΌ.jpg"); file.write(ваш Π½Π°Π±ΠΎΡ€ Π±Π°ΠΉΡ‚ΠΎΠ²); file.close(); //ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎ Π·Π°ΠΊΡ€Ρ‹Ρ‚ΡŒ Ρ„Π°ΠΉΠ» 
  • Denis, the problem is that I do not have a set of bytes. I can get an array of bytes and store it in a buffer, but I cannot save it as code bytes (encoded) to insert it into this line: "file.write (your set of bytes);" - Ivan Sorochinsky
  • And again, how to save the image code received byte so that you can copy it if necessary?) - Ivan Sorochinsky
  • You can view the image code using Far Manager (hex code) and copy it and paste it here: "file.write (your set of bytes);". But most likely it will be written as text (character array). - Ivan Sorochinsky
  • Need to use SVG. SVG is XML, and XML is known to be text. Copy, paste wherever you want, as you like. - Igor Kudryashov
  • @IgorKudryashov svg does not open, but if you rename it to jpg, the picture will open. (The code for creating svg files was written in the main message). - Ivan Sorochinsky

You can save the image as a "line" by re-encoding it into BASE64 and back, or use the SVG image format.

  • What is wrong with this answer? - Igor Kudryashov
  • Recoding it to BASE64 is an option. Do not tell me, what is the name of the necessary library and where to get it? From the SVG format to the manual, I can not copy the byte code. - Ivan Sorochinsky
  • If you have Java 8th, then it has a standard class java.util.Base64 . For earlier versions, you can use the org.apache.commons.codec.binary.Base64 class from the Apache Commons Codec . It's strange that you can't do it with SVG - it's practically XML, see. Scalable Vector Graphics - Igor Kudryashov
  • "You need to use SVG. SVG is XML, and XML is, as you know, text. Copy, paste wherever you want, as you like." Error opening file (via browser): This XML file doesn’t have any style information associated with it. The document tree is shown below. And again, if I open the code, it will no longer appear in its original form. - Ivan Sorochinsky
  • I do not understand your ultimate goal. What are you trying to do? To display in a browser, you need to svg "wrap" in html, as in other matters, and any other file other than html, because the browser is designed to view exactly html, and not something else. To view graphic files usually use special programs. About svg in html you can see here Embed SVG Directly Into HTML Pages - Igor Kudryashov