【问题标题】:Java ImageIO, convert Image to FileJava ImageIO,将图像转换为文件
【发布时间】:2013-10-27 17:36:35
【问题描述】:

所以我正在尝试获取 BufferedImage 并将其转换为 File 类型,但 ImageIO.write 只会让我将图像写入文件...我查看了源代码,但似乎无法找到一种将图像转换为文件的方法,而不是将其写入文件。

        // array of puzzle piece images
        BufferedImage puzzle_pieces[] = new BufferedImage[num_pieces];

        // make the pieces to the puzzle
        for (int current_row = 0; current_row < rows; current_row++)
        {
            for (int current_column = 0; current_column < columns; current_column++)
            {
                // initializes image array with pieces
                puzzle_pieces[count] = new BufferedImage(pieceWidth, pieceHeight, puzzle_image.getType());

                // draws the image for each piece  
                Graphics2D piece = puzzle_pieces[count++].createGraphics();  
                piece.drawImage(puzzle_image, 0, 0, pieceWidth, pieceHeight, pieceWidth * current_column, pieceHeight * current_row, 
                            pieceWidth * current_column + pieceWidth, pieceHeight * current_row + pieceHeight, null);  
                piece.dispose(); 
            }
        }

        // put pieces into array 
        for (int i = 0; i < num_pieces; i++) 
        {  
            //Put pieces into array of type File 
        } 

【问题讨论】:

  • 您究竟想通过将图像转换为文件来完成什么?
  • File 类型只表示文件的路径。将图像转换为文件没有多大意义。如果你想要一个文件,那么从头开始创建它:new File("image.png")
  • 我将图像分割成碎片......但我想将这些碎片加载到我的程序中,而不是将它们保存为 .jpeg。我有一个将碎片保存到文件的功能。但是,我需要将这些片段保留在我的程序中,而不是“图像”中。
  • 这没有任何意义。文件只是具有附加便利功能的文件路径。
  • 你说得对,谢谢!

标签: java arrays file javax.imageio


【解决方案1】:

这里不需要File。只需使用getSubimage(x,y,w,h)BufferedImage 分割成单独的图像。

该技术用于this answer 将图像(以BufferedImage 加载)划分为控件各个部分的子图像。鼠标悬停在“向右”控件上,显示用于显示焦点的变体图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-04
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 2010-09-12
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多