ImagesService.applyTransform上的间歇性IllegalArgumentException

时间:2018-05-01 13:57:00

标签: java google-app-engine

我在Google App Engine上IllegalArgumentException上调用applyTransform方法时,间歇性地开始ImagesService

java.lang.IllegalArgumentException:
    at com.google.appengine.api.images.ImagesServiceImpl.convertApplicationException(ImagesServiceImpl.java:397)
    at com.google.appengine.api.images.ImagesServiceImpl.applyTransform(ImagesServiceImpl.java:88)
    at com.google.appengine.api.images.ImagesServiceImpl.applyTransform(ImagesServiceImpl.java:65)
    at com.google.appengine.api.images.ImagesService$applyTransform.call(Unknown Source)

这是我的代码:

protected byte[] convertToJpeg(byte[] tiffByteArray) {
    ImagesService imagesService = ImagesServiceFactory.getImagesService()

    Image oldImage = ImagesServiceFactory.makeImage(tiffByteArray)

    Transform transform = ImagesServiceFactory.makeRotate(0)

    OutputSettings outputSettings = new OutputSettings(ImagesService.OutputEncoding.JPEG)
    outputSettings.setQuality(40)
    Image newImage = imagesService.applyTransform(transform, oldImage, outputSettings)
    return newImage.imageData
}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我从来没有弄清楚为什么会间歇性地为相同的输入输入IllegalArgumentException,但我将GAE应用程序升级到Java 8,这允许我使用ImageIO:

protected byte[] convertToJpeg(byte[] tiffByteArray) {
    final BufferedImage tif = ImageIO.read(new ByteArrayInputStream(tiffByteArray))

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()
    final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next()
    writer.setOutput(new MemoryCacheImageOutputStream(byteArrayOutputStream))

    JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null)
    jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT)
    jpegParams.setCompressionQuality(0.4f)

    writer.write(null, new IIOImage(tif, null, null), jpegParams)

    return byteArrayOutputStream.toByteArray()
}

对于TIFF,这需要额外的Gradle依赖:

compile group: 'com.github.jai-imageio', name: 'jai-imageio-core', version: '1.3.1'