2010-12-05 5 views
0

J'ai besoin de créer de nombreux fichiers tiff de grande taille dans java pour l'enregistrer comme tableau d'octets dans la base de données. J'ai seulement réussi à copier l'ancien fichier, le changer et en créer un nouveau - mais cela prend trop de temps pour créer le fichier (TIFFWriter.createTIFFFromImages). que puis-je faire?Comment créer un fichier TIFF?

public byte[] CreateTiff() throws IOException { 
    try{ 
     File orginialFile = new File("../Dist/dist/attachment/orginialTifFile.TIF"); 
     if(orginialFile!=null){ 
      TIFFReader reader = new TIFFReader(orginialFile); 
      int length = reader.countPages(); 
      BufferedImage[] images = new BufferedImage[length]; 

      for (int i = 0; i < length; i++) { 
       images[i] = (BufferedImage)reader.getPage(i); 
       int rgb = 0x000000; // black 

       Random rand = new Random(); 
       int x= rand.nextInt(images[i].getHeight()/2); 
       int y= rand.nextInt(images[i].getWidth()/2); 

       images[i].setRGB(x, y, rgb); 
      } 

      File newAttachmentFile = new File("../Dist/dist/attachment/tempFile.tif"); 
      TIFFWriter.createTIFFFromImages(images, newAttachmentFile); 
      byte[] b= getBytesFromFile(newAttachmentFile); 
      return b; 
     } 
    }catch(Exception e){ 
     System.out.println("failed to add atachment to request"); 
     e.printStackTrace(); 
     return null; 
    } 
    return null; 
} 

octet public static [] getBytesFromFile (fichier fichier) throws IOException { InputStream est = new FileInputStream (fichier); // Récupère la taille du fichier long length = file.length();

if (length > Integer.MAX_VALUE) { 
     return null; 
    } 

    // Create the byte array to hold the data 
    byte[] bytes = new byte[(int)length]; 

    // Read in the bytes 
    int offset = 0; 
    int numRead = 0; 
    while (offset < bytes.length 
      && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { 
     offset += numRead; 
    } 

    // Ensure all the bytes have been read in 
    if (offset < bytes.length) { 
     return null; 
    } 

    // Close the input stream and return bytes 
    is.close(); 
    return bytes; 
} 

Merci

+0

voir tiff ici: http://java.sun.com/products/java-media/jai/iio.html – zengr

+0

Pourquoi insérez-vous des points noirs aléatoires? – thejh

Répondre

0

Vous êtes en train d'écrire des données dans un fichier, puis la lecture de celui-ci, qui est inefficace. Essayez d'éviter cela. Si vous devez donner à la méthode un fichier, créez une fausse classe File qui renvoie un pipe, un arraywriter ou quelque chose comme ça comme son outputStream.

+0

Comment créer un faux fichier? arraywritter obtient le fichier path. – cls

Questions connexes