- Convert int[] binary array to image in java.
- 2 Answers 2
- Java: How to convert a binary string to an image?
- How to convert a binary image data to a jpg file in Java where every 10bits in the binary data represent a pixel?
- 2 Answers 2
- Java: Create an image from binary data (both image data and color palette)
- Specification
- Problem — Step 1
- Problem — Step 2
Convert int[] binary array to image in java.
It generates a Code38 Linear Barcode. It returns the int[] bars array. How do I convert that array into a barcode image. I don’t want to use any external jars/libs. If anyone has any better ideas for generating barcode images that would also be welcomed.
2 Answers 2
You can try the following code (take look at main method):
P.S. to make lines slimmer just change BARCODE_LINE_WIDTH variable.
import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.RenderedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; public class Code39 < private static final int BARCODE_LINE_WIDTH = 5; private static final int BARCODE_HEIGHT = 100; public static void main(String[] args) throws IOException < Code39 code39 = new Code39("123456789"); int[] barcode = code39.barcode(); int width = BARCODE_LINE_WIDTH * barcode.length; int height = BARCODE_HEIGHT; BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = bufferedImage.createGraphics(); for (int i = 0; i < barcode.length; i++) < if (barcode[i] == 0) < g2d.setColor(Color.WHITE); >else < g2d.setColor(Color.BLACK); >g2d.fillRect(i * BARCODE_LINE_WIDTH, 0, BARCODE_LINE_WIDTH, height); > g2d.dispose(); RenderedImage rendImage = bufferedImage; File file = new File("newimage.png"); ImageIO.write(rendImage, "png", file); > private String code; private int[] bars; public Code39(String code) < this.code = code.toUpperCase().trim(); bars = new int[(code.length() + 2) * 12]; this.buildSequence(); >public void buildSequence() < String encoded = "*" + code + "*"; int p = 0; for (int i = 0; i < encoded.length(); i++) < int[] sequence = mapSequence(encoded.charAt(i), i); System.arraycopy(sequence, 0, bars, p, sequence.length); p += sequence.length; >> public int[] mapSequence(char c, int pos) < HashMapsequence = new HashMap(); sequence.put('0', new int[]); sequence.put('1', new int[]); sequence.put('2', new int[]); sequence.put('3', new int[]); sequence.put('4', new int[]); sequence.put('5', new int[]); sequence.put('6', new int[]); sequence.put('7', new int[]); sequence.put('8', new int[]); sequence.put('9', new int[]); sequence.put('A', new int[]); sequence.put('B', new int[]); sequence.put('C', new int[]); sequence.put('D', new int[]); sequence.put('E', new int[]); sequence.put('F', new int[]); sequence.put('G', new int[]); sequence.put('H', new int[]); sequence.put('I', new int[]); sequence.put('J', new int[]); sequence.put('K', new int[]); sequence.put('L', new int[]); sequence.put('M', new int[]); sequence.put('N', new int[]); sequence.put('O', new int[]); sequence.put('P', new int[]); sequence.put('Q', new int[]); sequence.put('R', new int[]); sequence.put('S', new int[]); sequence.put('T', new int[]); sequence.put('U', new int[]); sequence.put('V', new int[]); sequence.put('W', new int[]); sequence.put('X', new int[]); sequence.put('Y', new int[]); sequence.put('Z', new int[]); sequence.put('-', new int[]); sequence.put('.', new int[]); sequence.put(' ', new int[]); sequence.put('$', new int[]); sequence.put('/', new int[]); sequence.put('+', new int[]); sequence.put('%', new int[]); sequence.put('*', new int[]); return sequence.get(c); > public int[] barcode() < return bars; >public String code() < return code; >public int calculateCheckDigit() < return 0; >>
And the result image(«newimage.png»):
Java: How to convert a binary string to an image?
I’m currently working on a tool to convert an image to a binary string and the other way around. To convert the image to binary I use the following method:
public void toText(String imagePath, String textPath) throws IOException < BufferedImage image = ImageIO.read(new File(imagePath)); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); ImageIO.write(image, "png", byteStream); byte[] bytes = byteStream.toByteArray(); PrintWriter writer = new PrintWriter(new File(textPath)); StringBuilder sb = new StringBuilder(); for(byte b : bytes) < sb.append(String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0')); >writer.write(sb.toString()); writer.close(); >
The output in the .txt file will then look like this: «10001001010100000100111001000111000011010000101000011010000. » I believe this code should work fine, please correct me if I’m wrong! Now I want to convert the string back to an image. To do so, I wrote this code:
public void toImage(String imagePath, String textPath) throws IOException < BufferedReader reader = new BufferedReader(new FileReader(new File(textPath))); String binary = reader.readLine(); reader.close(); byte[] bytes = new byte[binary.length() / 8]; for(int i = 0; i < bytes.length; i++) < bytes[i] = Byte.parseByte((String) binary.subSequence(i * 8, i * 8 + 8), 2); >BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes)); ImageIO.write(image, "png", new File(imagePath)); >
The problem with this code is, that «10001001» , for example, is bigger than the maximum size of the byte ( 137 ). That’s because «10001001» should in fact translate to the value -119 , using the 2’s complement representation. How can I solve this problem? How do I know when to use the «normal» binary representation and when to use 2’s complement? Thank you.
How to convert a binary image data to a jpg file in Java where every 10bits in the binary data represent a pixel?
I am receiving a stream of bits over the Ethernet . I am collecting the bits in a byte[] array in Java (I am collecting them in a byte[] because I think its relevant).The stream is a digitized image where every 10 bits represent a pixel. There are 1280*1024 pixels. Every pixel is represented by 10 bits. Hence, 1280*1024*10 = 13107200 bits = 1638400 bytes is the image size.
Sounds like an incredibly inefficient way to transfer a jpeg. Jpeg images are a compressed format, transferring it this way would be like using a bitmap but without the higher image quality.
Thank u @ Marc. I corrected my question. Is there a way to convert the 10bit to 8bit and make a jpg file out of it ?
If the source format is supported by Java you can just read it in with ImageIO.read and then write it out as a jpg with ImageIO.write . But I’m guessing your 10 bit images are not supported so you have to write your own code for reading them in.
Can you give us some more details about the input format? 10 bits per pixel is not enough information.
2 Answers 2
here’s the solution — but if the 10 bits represent actually 8 bits with some ‘nonsense’ in the other two bits its better to cut that like b=b>>2 — if your image is color then it sounds strange but use all 10 bits
int[] pix=new int[1280*1024]; for(i=0; i BufferedImage bim=new BufferedImage(1280, 1024, BufferedImage.TYPE_INT_RGB); bim.setRGB(0, 0, 1280, 1024, pix, 0, 1280); try < ImageIO.write(bim, "jpg", new File(path+".jpg")); >catch (IOException ex)
Thank you for your reply. Sorry for my ignorance. I have another question. I have the binary file with me with the image data. The smallest unit I can read in Java is a byte. I am reading it using ByteArrayOutputStream() object and then converting it to .toByteArray(). How can I possibly read every 10bits from that file?
Thank you. The pixels are actually intensities and hence I should probably make a gray scale image out of it. Is there a gray scale equivalent for setRGB ? Java has a parameter TYPE_USHORT_GRAY ,but setRGB accepts only int[]. How can this be handled ?
Java: Create an image from binary data (both image data and color palette)
I am trying to create an image from a binary file. The file contains a 32×32 icon and its corresponding 16 colors palette.
Specification
The icon is 512 bytes long. The icon is separated in 4×4 tiles. Each tile is 4×8 bytes. Here’s the 4×8 arrangement of bytes for a single tile:
11110000 11110000 11110000 11110000
00001111 00001111 00001111 00001111
11110000 11110000 11110000 11110000
00001111 00001111 00001111 00001111
11110000 11110000 11110000 11110000
00001111 00001111 00001111 00001111
11110000 11110000 11110000 11110000
00001111 00001111 00001111 00001111
1111 0000 1111 0000 1111 0000 1111 0000
0000 1111 0000 1111 0000 1111 0000 1111
1111 0000 1111 0000 1111 0000 1111 0000
0000 1111 0000 1111 0000 1111 0000 1111
1111 0000 1111 0000 1111 0000 1111 0000
0000 1111 0000 1111 0000 1111 0000 1111
1111 0000 1111 0000 1111 0000 1111 0000
0000 1111 0000 1111 0000 1111 0000 1111
Each 4 bit is the index of the color in the palette. The color palette is 32 bytes long and contains 16 colors. Each color is 16bits (5 for each component while the last is unused).
Problem — Step 1
I have managed to parse the image data into an array of 512 bytes and the color palette into an array of 32 bytes. But I am not really sure how to continue from hereon. I read all the image data bytes into a BitSet, but I am not sure if this is even useful. Also, I don’t know how to construct a color from two bytes. Any help/suggestions/comments? Thank you.
Problem — Step 2
int[] colors = new int[16*3]; for (int i = 0; i < 16; i++) < byte b1 = this.palette[i]; // byte 1: 5 bits of R and 3 bits of G byte b2 = this.palette[i+1]; // byte 2: 2 bits of G and 5 bits of B and 0. // colors are encoded in inverse order colors[i+2] = (b2 & 0x3E) >>> // red colors[i+1] = ((b1 & 0x07) > 6); // green colors[i] = (b1 & 0xF8) >>> 3; // blue > IndexColorModel cm = new IndexColorModel(5, 16*3, colors, 0, false, 0, DataBuffer.TYPE_BYTE);
Now I need to create a BufferedImage from the array of bytes I have read from the binary file using the above IndexColorModel. So far I have this:
DataBuffer buffer = new DataBufferByte(this.titleData, 32*32); int pixelStride = 4; //assuming r, g, b, skip, r, g, b, skip. int scanlineStride = 4*32; //no extra padding int[] bandOffsets = ; //r, g, b WritableRaster raster = Raster.createInterleavedRaster(buffer, 32, 32, scanlineStride, pixelStride, bandOffsets, null); boolean isAlphaPremultiplied = false; BufferedImage bim = new BufferedImage(cm, raster, isAlphaPremultiplied, null);
Taken from here. this.titleData is an array of 512 bytes that where read from the binary file. The above code throws the following exception: