Java ImageIO,将Image转换为File

时间:2013-10-27 17:36:36

标签: java arrays file javax.imageio

所以我正在尝试使用BufferedImage并将其转换为类型文件,但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 
        } 

1 个答案:

答案 0 :(得分:1)

此处不需要File。只需使用getSubimage(x,y,w,h)BufferedImage剪切成单独的图像即可。

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