0

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();
           }
    });

Text Output

This is the text output of the serial port.

EDIT 2:

Data that I got from the manufacturer of the machine.

Data from the manufacturer

EDIT 3:

Here is a drawn picture using Graphics Java library.

Serial number

  • See https://stackoverflow.com/a/18800845/18473608 –  Oct 24 '22 at 14:43
  • 1
    That's an 'ASCII art' image. It would be better to post the actual file as text. Please do that in the question (code tagged) – g00se Oct 24 '22 at 14:47
  • Incidentally, if that's a real "money counter machine" sending the serial number as ascii art instead of by 8bit digits would be monumentally inefficient, so I suspect that is just an additional feature: "send me that as ASCII art please" So you should be able to get it in the proper way – g00se Oct 24 '22 at 15:02
  • @g00se It was introduced by the user as in "I read the byte array from the machine and converted it to a binaryString" – Queeg Oct 24 '22 at 15:22
  • It's all quite confusing. e.g. *//new line byte, everytime it shows up I add a new line* but the value checked for is *not* the newline character, which would be 00001010 – g00se Oct 24 '22 at 15:40
  • Do not try to understand the low-level protocol of an embedded device. The OP did already and his interpretation is good enough to create ASCII art. All he needs is advice how to create a standard image. – Queeg Oct 24 '22 at 19:13
  • @g00se I edited the post with ASCII output. – Danilo Djurovic Oct 25 '22 at 12:38
  • I think you misunderstood. What I want to see is the total of stuff like *System.out.print(s1);* as *text*. You're still posting images. Please never post images of text. They are not searchable, we cannot copy-paste... Always copy-paste the text and format it properly. – g00se Oct 25 '22 at 12:47
  • @g00se Sorry, I edited the post again, I wrote the text directly to a txt file. Please take a look at the file in the post. – Danilo Djurovic Oct 25 '22 at 13:25
  • That's still wrong. That is a *binary* file, *not* a text file, despite your having given it that extension. That could *not* have been produced from string ```s1``` in your source. The serial number E16625824B appears at offset 0x141 in that file. What is your [goal](http://technojeeves.com/joomla/index.php/free/117-smart-questions]GOAL[/url])? – g00se Oct 25 '22 at 13:29
  • @g00se The goal is to make a user interface where the user can receive counted data from the machine and serial numbers of notes(OCR value and picture). From there he can print the data and/or save it in a transactions database. – Danilo Djurovic Oct 26 '22 at 07:36
  • @g00se I contacted the manufacturer of the machine and got the data(link in the attachment) from them. It consists of C/C++ files and and an .ocx file. Can I use this in my application? I edited the post with the files I got. – Danilo Djurovic Oct 26 '22 at 13:52
  • You *could* use those files via JNA but it would be easier to do it another way. You *do* realise you've *already* successfully read the serial number..? – g00se Oct 26 '22 at 14:15
  • I guess so :) I have a couple of more money counters to integrate with this app, and usually, for all of them, there is a c/c++ library and an ocx file. What is your opinion on this, should I use the JNA or do something similar as I did with this one? What could be the easier solution in your opinion? Thanks again – Danilo Djurovic Oct 27 '22 at 20:54
  • I would go with what you're doing already then you've got a simpler cross-platform solution – g00se Oct 27 '22 at 21:13

1 Answers1

2

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);
        }
    }
}
Queeg
  • 7,748
  • 1
  • 16
  • 42
  • Thanks for the answer, I will try it and let you know the results. – Danilo Djurovic Oct 25 '22 at 12:38
  • This might help you understand PBM format https://stackoverflow.com/a/71247389/2836621 – Mark Setchell Oct 27 '22 at 08:43
  • @Hiran Chaudhuri I tried following your instructions and tried the drawString method, but I only get the first row of my String shown in the picture. I then tried spliting the string on each new line to make an array and use a for loop to iterate through the array and call drawString on each iteration, but then my lines get drawn one on top of other, they overlap. Also, I'm not sure I understand how to draw a black rectangle for each 1 in the String, can you show me a code example for that? Thanks – Danilo Djurovic Oct 27 '22 at 19:40
  • I never suggested using drawString. But I added some code. – Queeg Oct 27 '22 at 20:39
  • Thanks for the help, but I managed to solve it in the meantime with drawString method. The problem was with the y-axis, I didn't increase it in the for loop while I was drawing the lines, that's why lines were overlapping. I didn't need to paint 1s to black, I just set the Graphics2d color to white, and I drew the picture. I set the font so small that it is not recognizable that there are 0s or 1s. The picture is looking pretty good actually. Once again, thanks for your help. I edited the post with the final picture of the serial number in png format. – Danilo Djurovic Oct 27 '22 at 20:50