Как создать файл TIFF?

Мне нужно создать много разных больших файлов tiff в java, чтобы сохранить его как массив байтов в базе данных. Мне удалось скопировать только старый файл, изменить его и создать новый, но создание файла занимает слишком много времени (TIFFWriter.createTIFFFromImages). что я могу делать?

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 {InputStream is = new 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;
}

Спасибо


person cls    schedule 05.12.2010    source источник
comment
см. tiff здесь: java.sun.com/products/java-media/ jai / iio.html   -  person zengr    schedule 05.12.2010
comment
Почему вы вставляете случайные черные точки?   -  person thejh    schedule 05.12.2010


Ответы (1)


Вы записываете данные в файл, а затем читаете из него, что неэффективно. Постарайтесь этого избежать. Если вам нужно передать методу File, создайте поддельный класс File, который возвращает канал, устройство записи массива или что-то подобное в качестве outputStream.

person thejh    schedule 05.12.2010
comment
Как создать фальшивый файл? arraywritter получить путь к файлу. - person cls; 16.12.2010