如何创建 TIFF 文件?

发布于 2024-10-06 06:19:50 字数 2059 浏览 8 评论 0原文

我需要在 java 中创建许多不同的大型 tiff 文件,以便将其保存为数据库中的字节数组。 我只设法复制旧文件,更改它并创建新文件 - 但需要太多时间来创建文件(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{ 输入流 = new FileInputStream(file); // 获取文件大小 长长度 = 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;
}

谢谢

I need to create many different large tiff files in java for save it as byte array in dataBase.
I only managed to copy old file, change it and create new one - but it take too much time to crete the file(TIFFWriter.createTIFFFromImages).
what can I do?

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;
}

public static byte[] getBytesFromFile(File file) throws IOException{
InputStream is = new FileInputStream(file);
// Get the size of the file
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;
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霞映澄塘 2024-10-13 06:19:50

您将数据写入文件,然后从中读取数据,这是低效的。尽量避免这种情况。如果您必须为该方法提供一个 File,请创建一个假 File 类,该类返回一个管道、一个数组写入器或类似的东西作为其输出流。

You're writing data to a file and then reading from it, that's inefficient. Try to avoid that. If you have to give the method a File, create a fake File class that returns a pipe, an arraywriter or something like that as its outputStream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文