Home > Mobile >  How to convert a Binary String into Gray scale image?
How to convert a Binary String into Gray scale image?

Time:10-28

I'm reading a serial port from a money counter machine and I'm expecting to get a serial number of a banknote sent from the money counter.

I read the byte array from the machine and converted it to a binaryString using the code below:

public void serialEvent(SerialPortEvent serialPortEvent) {

        ArrayList<String> list = new ArrayList<>();
        String s1 = new String();

        if (serialPortEvent.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {
            return;
        }
        byte[] readBuffer = new byte[comPort.bytesAvailable()];
        int numRead = comPort.readBytes(readBuffer, readBuffer.length);
        //System.out.println("Read "   numRead   " bytes.");

        for (Byte b : readBuffer) {

            //image is more than 500 bytes, all other data is less than 500
            if (numRead <= 500) {
                break;
            }

            s1 = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');

            //new line byte, everytime it shows up I add a new line
            if (s1.equals("01000101")) {
                System.out.println();
                continue;
            }

            System.out.print(s1);

The picture below is the screenshot from s1 String that I got from System.out and as you can see there is the serial number represented by 1s, and the background is represented by 0s.

enter image description here

My question now is how to convert this String to an image file? I'm guessing I need to make an 1d or 2d array from this String and make an image where 0s represent white pixels and 1s represent black pixels, but I'm not sure how to do this and I need some help?

Thanks in advance.

EDIT:

I can get the ASCII output using this code:

public void serialEvent(SerialPortEvent serialPortEvent) {

           InputStream in = comPort.getInputStream();
           Reader in2 = new InputStreamReader(in, StandardCharsets.US_ASCII);
            try
            {
                for (int j = 0; j < 10000;   j) {
                    System.out.print((char)in2.read());
                    in.close();
                }
            } catch (Exception e) { e.printStackTrace(); }
            comPort.closePort();
           }
    });

Serial number

CodePudding user response:

PBM File Format

You can almost directly write your ASCII art string into a PBM file. Check the file format here: https://netpbm.sourceforge.net/doc/pbm.html

Other file format

If you prefer to create BMP | GIF | JPEG | PNG | TIFF | WBMP (the formats supported by ImageIO), follow this procedure:

Converting from your ascii strings could look like this:

Graphics g = ...
// assuming your string values are stored in the array strings
for(int y=0; y<strings.length, y  ) {
    String line = strings[y];
    for(int x=0; x<line.length(); x  ) {
        if (line.charAt(x)=='1') {
            g.setColor(Color.black);
            g.fillRect(x, y, 1, 1);
        }
    }
}
  • Related