ข้อผิดพลาด: java.lang.UnsupportedOperationException: ไม่มีข้อมูลรูปภาพเมื่อใช้ BlobStore และ Image API ของ App Engine

ฉันต้องการดึงข้อมูลความสูงและความกว้างของรูปภาพที่อัปโหลดโดยใช้ App Engine BlobStore สำหรับการค้นหาว่าฉันใช้รหัสต่อไปนี้:

try {
            Image im = ImagesServiceFactory.makeImageFromBlob(blobKey);

            if (im.getHeight() == ht && im.getWidth() == wd) {
                flag = true;
            }
        } catch (UnsupportedOperationException e) {

        }

ฉันสามารถอัปโหลดรูปภาพและสร้าง BlobKey ได้ แต่เมื่อส่ง Blobkey ไปที่ makeImageFromBlob() มันจะสร้างข้อผิดพลาดต่อไปนี้:

java.lang.UnsupportedOperationException: ไม่มีข้อมูลรูปภาพ

วิธีแก้ปัญหานี้หรือวิธีอื่นในการค้นหาความสูงและความกว้างของรูปภาพโดยตรงจาก BlobKey


person Master Mind    schedule 25.07.2012    source แหล่งที่มา


คำตอบ (3)


วิธีการส่วนใหญ่บนอิมเมจนั้นปัจจุบันจะโยน UnsupportedOperationException ดังนั้นฉันจึงใช้ com.google.appengine.api.blobstore.BlobstoreInputStream.BlobstoreInputStream เพื่อจัดการข้อมูลจาก blobKey นั่นเป็นวิธีที่ฉันสามารถหาความกว้างและความสูงของภาพได้

byte[] data = getData(blobKey);
Image im = ImagesServiceFactory.makeImage(data);
if (im.getHeight() == ht && im.getWidth() == wd) {}
private byte[] getData(BlobKey blobKey) {
    InputStream input;
    byte[] oldImageData = null;
    try {
        input = new BlobstoreInputStream(blobKey);
                ByteArrayOutputStream bais = new ByteArrayOutputStream();
        byte[] byteChunk = new byte[4096];
        int n;
        while ((n = input.read(byteChunk)) > 0) {
            bais.write(byteChunk, 0, n);
        }
        oldImageData = bais.toByteArray();
    } catch (IOException e) {}

    return oldImageData;

}
person Master Mind    schedule 26.07.2012

หากคุณใช้ Guava ได้ การนำไปปฏิบัติก็จะง่ายกว่า:

public static byte[] getData(BlobKey blobKey) {
    BlobstoreInputStream input = null;
    try {
        input = new BlobstoreInputStream(blobKey);
        return ByteStreams.toByteArray(input);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        Closeables.closeQuietly(input);
    }
}

ส่วนที่เหลือยังคงเหมือนเดิม

person Nacho Coloma    schedule 18.09.2012

ความเป็นไปได้อีกอย่างหนึ่งคือทำการแปลงรูปภาพโดยไร้ประโยชน์ (โดยหมุน 0 องศา)

Image oldImage = ImagesServiceFactory.makeImageFromFilename(### Filepath ###);
Transform transform = ImagesServiceFactory.makeRotate(0);
oldImage = imagesService.applyTransform(transform,oldImage);

หลังจากการแปลงดังกล่าว คุณอาจได้ความกว้างและความสูงของรูปภาพตามที่คาดไว้:

oldImage.getWidth();

แม้ว่าจะได้ผล แต่การเปลี่ยนแปลงนี้ก็ส่งผลต่อประสิทธิภาพในเชิงลบ ;)

person Jokus    schedule 14.10.2016