因为ImageIo在默认情况下不支持TIF图像,所以我添加了imageio 1.1.jar(在这里获得- http://www.findjar.com/jar/geoserver/jai/jars/jai_imageio-1.1-alpha.jar.html)。现在ImageIO可以注册这个插件了,我通过这样做检查了这个插件
String[] writerNames = ImageIO.getWriterFormatNames();
writerNames中有TIF,这意味着ImageIO已经注册了插件。
我像这样调整图像的大小
Map resizeImage(BufferedImage imageData, int width, int height, String imageFormat){
BufferedImage thumbnail = Scalr.resize(imageData, Scalr.Method.SPEED, Scalr.Mode.FIT_EXACT ,
width, height, Scalr.OP_ANTIALIAS);
String[] writerNames = ImageIO.getWriterFormatNames();
ByteArrayOutputStream baos = new ByteArrayOutputStream()
ImageIO.write(thumbnail, imageFormat, baos)
baos.flush()
byte[] imageBytes = baos.toByteArray()
baos.close()
return [imageBytes:imageBytes, imageFormat:imageFormat]
}
String encodeImageToBase64(byte[] imageData){
return Base64.encodeBase64String(imageData)
}
BufferedImage getBufferedImage(byte[] imageData){
ByteArrayInputStream bais = new ByteArrayInputStream(imageData)
BufferedImage bImageFromConvert = ImageIO.read(bais)
bais.close()
return bImageFromConvert
}
String resizeToDimensions(byte[] imageData, String imageFormat, int width, int height){
def bimg = getBufferedImage(imageData)
Map resizedImageData = resizeImage(bimg, width, height, imageFormat)
return encodeImageToBase64(resizedImageData.imageBytes)
}现在,我显示的图像就像这样的< img src = "data:image/tif;base64,TU0AKgAAAAgADAEAAAMAAA...." />,据我所知,base64字符串通常以/9j/开头(可能我错了),所以我无法加载url消息(悬停时)。当我附加/9j/时。我得到一个错误-“图像损坏或截断”。我想不出这里有什么问题,请帮帮忙。
发布于 2012-12-28 02:12:07
乍一看,您对Data URI format的使用看起来是正确的--试着精确地缩小故障的范围。
我会推荐:
走到这一步应该可以回答大多数问题。
实际上,现在我想了想,尝试使用ImageIO将图像读取到BufferedImage中,然后使用imgscalr处理它,然后立即调用ImageIO.write并尝试将其写出到其他地方的新TIF中,并确保ImageIO解码/编码过程正常工作。
希望这能有所帮助!
https://stackoverflow.com/questions/14052111
复制相似问题