2010-12-05 17 views
0

私は、Javaで多くの異なる大きなTIFFファイルを作成して、dataBaseのバイト配列として保存する必要があります。 古いファイルをコピーして変更して新しいファイルを作成することしかできませんでしたが、ファイルを作成するには時間がかかります(TIFFWriter.createTIFFFromImages)。 私は何ができますか?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; 
} 

パブリック静的バイト[] getBytesFromFile(ファイルのファイル)にIOExceptionをスロー{ 入力ストリームは=新しいFileInputStreamを(ファイル)です。 //ファイルのサイズを取得します 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; 
} 

おかげ

+0

ここでtiffを参照してください:http://java.sun.com/products/java-media/jai/iio.html – zengr

+0

ランダムな黒点を挿入するのはなぜですか? – thejh

答えて

0

あなたがファイルにデータを書き込み、それから読んでいるが、それは非効率的です。それを避けてください。メソッドにFileを渡す必要がある場合は、そのoutputStreamとしてパイプや配列ライターなどを返す偽のFileクラスを作成します。

+0

どのように偽のファイルを作成しますか? arraywritter get pathファイル。 – cls

関連する問題