0

I'm using Base91 to store an image as a String in a mysql database. When I retrieve the string to convert it back to an image, the string seems to have been corrupted by the database.

I'm sure it's being corrupted in the database because I did an isolated test on a single device of taking a picture, converting it to a Base91 byte[], converting the byte[] to a string (using Latin1), converting the string back to byte[], then to an image, and it worked. When I try that with a server in the middle, the images end up broken.

I've tried changing the collation on the specific column on the database from utf8 to ascii to latin1, no luck.

Code:

  1. Encoding Picture:

    File file1 = new File(path);
    FileInputStream in = new FileInputStream(file1);
    
    byte[] imagesBytes = new byte[(int) file1.length()];
    in.read(imagesBytes, 0, (int) file1.length());
    
    _picture = new String(Base91.encode(imagesBytes), Base91.CHARSET);
    //appointment.addPicture(new String(Base91.encode(imagesBytes), Base91.CHARSET));
    
  2. Storing encoded image:

     JsonObject jsonx = new JsonObject();
     jsonx.addProperty("picture", request.getParameter("picture"));
    
     db.insert("AppointmentPicture",
                "(AppointmentID, picture)values(?,?)",
                new Object[]{appointment.getDbID(), jsonx.toString()},
                false);
    
  3. Retrieving the image:

    ResultSet rSet = db.query("AppointmentPicture", new String[]{"picture"}, "AppointmentID = ?", new Object[]{appointmentID});
        System.out.println("pictures grabbed");
    
        JsonObject json = new JsonObject();
        int counter = 0;
    
        while (rSet.next()) {
            counter++;
            json.addProperty(String.valueOf(counter), rSet.getString("picture"));
    
        }
    
        json.addProperty("count", String.valueOf(counter));
    
        response.getWriter().write(json.toString());
        System.out.println("pictures returned");
    
  4. Decoding Image:

     String serverResponse = in.readLine();
    
     JSONObject json = new JSONObject(serverResponse);
    
     int count = Integer.parseInt(json.getString("count"));
    
     for(int i = 0; i < count; i++){
    
         JSONObject j = new JSONObject(json.getString(String.valueOf(i + 1)));
         byte[] picBytes = j.getString("picture").getBytes(Base91.CHARSET);
         File file = new File(imageDirectory, String.valueOf(System.currentTimeMillis()) + ".jpeg");
         BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
         out.write(picBytes, 0, picBytes.length);
    
     }
    
phuclv
  • 37,963
  • 15
  • 156
  • 475
BiGGZ
  • 503
  • 4
  • 17
  • so, if you try the simple test of taking the base91 encoded string, sending it to the database and reading it back out, how does it compare to the original string? – jtahlborn Dec 06 '16 at 00:03
  • thats almost exactly what im doing, write with one device, read with another. I cant copy/paste the raw string onto notepad and do a side by side comparison because it just freezes when i try to do that...too much text i guess... – BiGGZ Dec 06 '16 at 00:08
  • do it programmatically from the same device. – jtahlborn Dec 06 '16 at 00:14
  • ok, wil let you know how that goes. Il also update my question with the code – BiGGZ Dec 06 '16 at 00:24
  • @jtahlborn, please see my updated question with code. Instead of adding the image strings to a collection(which is my goal), im using a single var "_picture" for the isolation test – BiGGZ Dec 06 '16 at 21:51
  • what did you find out when you compared the string you put into the db to the string you pulled out of the db? – jtahlborn Dec 07 '16 at 00:20
  • some characters seem to get lost during all the parsing.So how do i preserve my string? – BiGGZ Dec 07 '16 at 11:34
  • Furthermore, when i eliminate the storage part, the pictures get reconstructed properly, but when i write, then read, the bytes are all screwed up – BiGGZ Dec 07 '16 at 12:10
  • 1
    I found my error, wasnt decoding properly, this `byte[] picBytes = j.getString("picture").getBytes(Base91.CHARSET);`, should have been this `byte[] picBytes = Base91.decode(j.getString("picture").getBytes(Base91.CHARSET));`....dont know how i missed that – BiGGZ Dec 07 '16 at 14:09

0 Answers0