3 Replies Latest reply: Mar 8, 2010 3:20 AM by 843802 RSS

    convert tiff to jpeg using jai

    843802
      hey. i'm trying to convert a tiff file to a jpeg.I have the tiff as a byte array, O need the jpeg as a byte array too. i just can't seem to find a way to convert it to jpeg, don't understand the javadoc at all!
      i get that i should use the JAI. create, but the string parameters aren't described anywhere. I sort of guessed the start of how the code should be
      ByteArrayInputStream bais = new ByteArrayInputStream(myImage);
      RenderedOp inputfile = JAI.create("stream", bais);
      but then what next?

      i thought something along the line of
      RenderedOp result = JAI.create("convert?", inputfile, "jpg");
       // get the byte[] from result, somehow
      I'm sure it's not a difficult thing but I just can't figure it out

      Edited by: dilutza on May 27, 2008 3:27 AM
        • 1. Re: convert tiff to jpeg using jai
          843802
          hi dilutza,
          can u tell me wht u want to do exactly?
          i think, u want to convert pages of multiPageTiff image to single jpeg images, if m i right....

          if u want to convert tiff to jpeg, check out followin code...
          i wrote this 4 tiff to png conversion ...
           public static RenderedImage[] readMultiPageTiff(String *fileName*)throws IOException{
                 File file = new File(fileName);
                 SeekableStream seekableStream = new FileSeekableStream(file);
                 ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", seekableStream, null);
                 int numPages = decoder.getNumPages();
                 RenderedImage image[]= new RenderedImage[numPages];
                 int count = 0;
                 for(int i=0;i<decoder.getNumPages();i++){
                     image[i] = decoder.decodeAsRenderedImage(i);
                     count++;
                 }
                 
                 String newFolderName;
                 String s3 = fileName;
                 String [] temp = null;
                 temp = s3.split("\\.");
                 
                 int j;
                     j = 0;
                     do{
                           newFolderName = temp[j];
                           String spoonFeeding = newFolderName;
                           File f = new File(spoonFeeding);
                           f.mkdirs();
                           j++;
                     }while (j<1);
                 
                 for (int i = 0; i < count; i++) {
                     RenderedImage page = decoder.decodeAsRenderedImage(i);
                     File fileObj = new File(newFolderName+"/" + (i+1) + ".png");
                     System.out.println("Saving " + fileObj.getCanonicalPath());
                     ParameterBlock parBlock = new ParameterBlock();
                     parBlock.addSource(page);
                     parBlock.add(fileObj.toString());
                     parBlock.add("png");
                     RenderedOp renderedOp = JAI.create("filestore",parBlock);
                     renderedOp.dispose();
                 }
                 return image;
              }
          jus pass the path of file to method... method read that tiff image ... create a new folder in same path names input...
          and convert the tiff pages to png image...
          • 2. Re: convert tiff to jpeg using jai
            843802
            if u want d same.. thn change d code as per ur requirement ...
            i use
                            do{
                             newFolderName = temp[j];
                             String spoonFeeding = newFolderName;
                             File f = new File(spoonFeeding);
                             f.mkdirs();
                             j++;
                       }while (j<1);
            this code cause single time i passd no. of tiff images ...

            .
            .
            .
            Regards,
            dipak.

            Edited by: midnight_coder on Jun 17, 2008 8:12 AM
            • 3. Re: convert tiff to jpeg using jai
              843802
              Hi,

              I found this post useful, but wanted to read a tiff embedded in a SQL database and write that back to a JPEG in the same database.

              This is how I did it (copied direct from a swing app)
              @Action
                  public void RUNCONVERT() {
                      String SQLUrl = "jdbc:mysql://127.0.0.1/somedatabase";
                      Connection SqlConnection = null;
                      String driverName = "org.gjt.mm.mysql.Driver"; // MySQL MM JDBC driver
              
                      try {
                          Class.forName(driverName);
                          SqlConnection = DriverManager.getConnection(SQLUrl, "user", "password");
                      } catch (ClassNotFoundException e) {
                          jTextArea1.setText(jTextArea1.getName().toString() + "\n" + e);
                      } catch (SQLException e) {
                          jTextArea1.setText(jTextArea1.getName().toString() + "\n" + e);
                      }
              
                      //count the rows in the database
                      String sqlcmd = "SELECT COUNT(ROWID) FROM POOL;";
                      int count = 0;
              
                      //execute the command
                      try {
                          Statement stmt = SqlConnection.createStatement();
                          ResultSet rs1 = stmt.executeQuery(sqlcmd);
                          rs1.first();
                          count = rs1.getInt(1);
                      } catch (SQLException e) {
                          jTextArea1.setText(jTextArea1.getName().toString() + "\n" + e);
                      }
              
                      byte imagetiff[] = null;
                      byte imagejpeg[] = null;
              
              
                      for (int c = 0; c < count; c++) {
                          //Read in the tiff
                          sqlcmd = "SELECT imagetiff FROM POOL WHERE rowid='" + String.valueOf(c) + "';";
                          //execute the command
                          try {
                              Statement stmt = SqlConnection.createStatement();
                              ResultSet rs1 = stmt.executeQuery(sqlcmd);
                              rs1.first();
                              imagetiff = rs1.getBytes(1);
                          } catch (SQLException e) {
                              jTextArea1.setText(jTextArea1.getName().toString() + "\n" + e);
                          }
              
                          //Convert it to jpeg
                          try {
                              InputStream is = new ByteArrayInputStream(imagetiff);
                              ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", is, null);
                              RenderedImage ri = decoder.decodeAsRenderedImage();
                              ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                              ImageIO.write(ri, "JPEG", outputStream);
              
                              sqlcmd = "UPDATE POOL SET imagejpeg" +
                                      "=? WHERE rowid='" + String.valueOf(c) + "'";
              
                              //execute the insert command
                              try {
                                  PreparedStatement pstmt = SqlConnection.prepareStatement(sqlcmd);
              
                                  // When done, get bytes and prepare statement
                                  byte bytes[] = outputStream.toByteArray();
                                  pstmt.setBytes(1, bytes);
                                  pstmt.executeUpdate();
                              } catch (Exception e) {
                                  jTextArea1.setText(jTextArea1.getName().toString() + "\n" + e);
                              }
              
                          } catch (Exception e) {
                              jTextArea1.setText(jTextArea1.getName().toString() + "\n" + e);
                          }
                      }
              
              
                  }