2013-01-24 4 views
0

사용자 프로필 사진을 보유하고있는 엔티티가 있습니다. 일을 단순하게 유지하기 위해 게재 URL을 저장하기 만하면됩니다. 내 사용자가 이미지를 자르도록 허용하고 있습니다. 이렇게하려면 검색 URL에서 이미지를 가져와 com.google.appengine.api.images.Image;을 만듭니다.이 이미지는 바이트 배열로 생성되었으므로 BLOB 키가 없습니다. 이 이미지를 얼룩으로 저장하고 검색 URL을 얻는 가장 쉬운 방법은 무엇입니까?URL에서 이미지를 만들고 데이터 저장소에 저장하십시오.

URL url = new URL(currentProfile.getProfilePictureUrl()); 
InputStream input = url.openStream(); 
byte[] byteArray = IOUtils.toByteArray(input); 
Image newImage = ImagesServiceFactory.makeImage(byteArray); 

int imageWidth = newImage.getWidth(); 
int imageHeight = newImage.getHeight(); 

ImagesService imagesService = ImagesServiceFactory.getImagesService(); 
float lx = (float)x/(float)imageWidth; 
float ty = (float)y/(float)imageHeight; 
float rx = (float)x2/(float)imageWidth; 
float by = (float)y2/(float)imageHeight; 
Transform resize = ImagesServiceFactory.makeCrop(lx, ty, rx, by); 

Image transImage = imagesService.applyTransform(resize, newImage); 

BlobstoreService blobStoreService = BlobstoreServiceFactory.getBlobstoreService(); 

//This doesn't work because transImage.getBlobKey() is null 
ServingUrlOptions options = ServingUrlOptions.Builder.withBlobKey(transImage.getBlobKey()); 

답변

4

write data to blobstore programmatically 수 있습니다. 이 작은 스 니펫을 사용합니다.

private BlobKey saveToBlobstore(String contentType, String imageName, byte[] imageData) throws IOException { 
    // Get a file service 
    FileService fileService = FileServiceFactory.getFileService(); 

    // Create a new Blob file and set the name to contain ref to UserImage 
    AppEngineFile file = fileService.createNewBlobFile(contentType, imageName); 

    // Open a channel to write to it 
    FileWriteChannel writeChannel = fileService.openWriteChannel(file, true); 

    writeChannel.write(ByteBuffer.wrap(imageData)); 
    writeChannel.closeFinally(); 

    // return the BlobKey 
    return fileService.getBlobKey(file); 
} 
+0

어떻게 Blobkey를 검색합니까? 내가 Blobkey를 얻은 후에 그것을 속성으로 저장해야합니까? – serj

관련 문제