-1

所以我试图获取一个 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 
        } 
4

1 回答 1

1

File这里不需要。只需BufferedImage使用getSubimage(x,y,w,h).

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

于 2013-10-27T21:56:18.170 回答