有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将图像从文件读/写到BuffereImage的最快方法?

  1. 在Java/Grails中,将图像从文件读入BuffereImage的最快方法是什么
  2. 在Java/Grails中,将图像从BuffereImage写入文件的最快方法是什么

我的变体(阅读):

byte [] imageByteArray = new File(basePath+imageSource).readBytes()
InputStream inStream = new ByteArrayInputStream(imageByteArray)
BufferedImage bufferedImage = ImageIO.read(inStream)

我的变体(写):

BufferedImage bufferedImage = // some image
def fullPath = // image page + file name
byte [] currentImage

try{

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write( bufferedImage, "jpg", baos );
    baos.flush();
    currentImage = baos.toByteArray();
    baos.close();

    }catch(IOException e){
        System.out.println(e.getMessage());
    }       
   }    


def newFile = new FileOutputStream(fullPath)
newFile.write(currentImage)
newFile.close()

共 (0) 个答案