|
| 1 | +/** |
| 2 | + * Copyright 2015 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.appengine.images; |
| 18 | +import com.google.appengine.api.blobstore.BlobKey; |
| 19 | +import com.google.appengine.api.blobstore.BlobstoreService; |
| 20 | +import com.google.appengine.api.blobstore.BlobstoreServiceFactory; |
| 21 | +import com.google.appengine.api.images.Image; |
| 22 | +import com.google.appengine.api.images.ImagesService; |
| 23 | +import com.google.appengine.api.images.ImagesServiceFactory; |
| 24 | +import com.google.appengine.api.images.Transform; |
| 25 | +import com.google.appengine.tools.cloudstorage.GcsFileOptions; |
| 26 | +import com.google.appengine.tools.cloudstorage.GcsFilename; |
| 27 | +import com.google.appengine.tools.cloudstorage.GcsService; |
| 28 | +import com.google.appengine.tools.cloudstorage.GcsServiceFactory; |
| 29 | +import com.google.appengine.tools.cloudstorage.RetryParams; |
| 30 | + |
| 31 | +import java.io.File; |
| 32 | +import java.io.FileInputStream; |
| 33 | +import java.io.IOException; |
| 34 | +import java.io.PrintWriter; |
| 35 | +import java.nio.ByteBuffer; |
| 36 | +import java.nio.channels.FileChannel; |
| 37 | + |
| 38 | +import javax.servlet.http.HttpServlet; |
| 39 | +import javax.servlet.http.HttpServletRequest; |
| 40 | +import javax.servlet.http.HttpServletResponse; |
| 41 | + |
| 42 | +// [START example] |
| 43 | +@SuppressWarnings("serial") |
| 44 | +public class ImagesServlet extends HttpServlet { |
| 45 | + final String bucket = "YOUR-BUCKETNAME-HERE"; |
| 46 | + |
| 47 | + // [START gcs] |
| 48 | + private final GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder() |
| 49 | + .initialRetryDelayMillis(10) |
| 50 | + .retryMaxAttempts(10) |
| 51 | + .totalRetryPeriodMillis(15000) |
| 52 | + .build()); |
| 53 | + // [END gcs] |
| 54 | + |
| 55 | + @Override |
| 56 | + public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 57 | + |
| 58 | + //[START original_image] |
| 59 | + // Read the image.jpg resource into a ByteBuffer. |
| 60 | + FileInputStream fileInputStream = new FileInputStream(new File("WEB-INF/image.jpg")); |
| 61 | + FileChannel fileChannel = fileInputStream.getChannel(); |
| 62 | + ByteBuffer byteBuffer = ByteBuffer.allocate((int)fileChannel.size()); |
| 63 | + fileChannel.read(byteBuffer); |
| 64 | + |
| 65 | + byte[] imageBytes = byteBuffer.array(); |
| 66 | + |
| 67 | + // Write the original image to Cloud Storage |
| 68 | + gcsService.createOrReplace( |
| 69 | + new GcsFilename(bucket, "image.jpeg"), |
| 70 | + new GcsFileOptions.Builder().mimeType("image/jpeg").build(), |
| 71 | + ByteBuffer.wrap(imageBytes)); |
| 72 | + //[END original_image] |
| 73 | + |
| 74 | + //[START resize] |
| 75 | + // Get an instance of the imagesService we can use to transform images. |
| 76 | + ImagesService imagesService = ImagesServiceFactory.getImagesService(); |
| 77 | + |
| 78 | + // Make an image directly from a byte array, and transform it. |
| 79 | + Image image = ImagesServiceFactory.makeImage(imageBytes); |
| 80 | + Transform resize = ImagesServiceFactory.makeResize(100, 50); |
| 81 | + Image resizedImage = imagesService.applyTransform(resize, image); |
| 82 | + |
| 83 | + // Write the transformed image back to a Cloud Storage object. |
| 84 | + gcsService.createOrReplace( |
| 85 | + new GcsFilename(bucket, "resizedImage.jpeg"), |
| 86 | + new GcsFileOptions.Builder().mimeType("image/jpeg").build(), |
| 87 | + ByteBuffer.wrap(resizedImage.getImageData())); |
| 88 | + //[END resize] |
| 89 | + |
| 90 | + //[START rotate] |
| 91 | + // Make an image from a Cloud Storage object, and transform it. |
| 92 | + BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); |
| 93 | + BlobKey blobKey = blobstoreService.createGsBlobKey("gs://" + bucket + "/image.jpeg"); |
| 94 | + Image blobImage = ImagesServiceFactory.makeImageFromBlob(blobKey); |
| 95 | + Transform rotate = ImagesServiceFactory.makeRotate(90); |
| 96 | + Image rotatedImage = imagesService.applyTransform(rotate, blobImage); |
| 97 | + |
| 98 | + // Write the transformed image back to a Cloud Storage object. |
| 99 | + gcsService.createOrReplace( |
| 100 | + new GcsFilename(bucket, "rotatedImage.jpeg"), |
| 101 | + new GcsFileOptions.Builder().mimeType("image/jpeg").build(), |
| 102 | + ByteBuffer.wrap(rotatedImage.getImageData())); |
| 103 | + //[END rotate] |
| 104 | + |
| 105 | + // Output some simple HTML to display the images we wrote to Cloud Storage |
| 106 | + // in the browser. |
| 107 | + PrintWriter out = resp.getWriter(); |
| 108 | + out.println("<html><body>\n"); |
| 109 | + out.println("<img src='//storage.cloud.google.com/" + bucket |
| 110 | + + "/image.jpeg' alt='AppEngine logo' />"); |
| 111 | + out.println("<img src='//storage.cloud.google.com/" + bucket |
| 112 | + + "/resizedImage.jpeg' alt='AppEngine logo resized' />"); |
| 113 | + out.println("<img src='//storage.cloud.google.com/" + bucket |
| 114 | + + "/rotatedImage.jpeg' alt='AppEngine logo rotated' />"); |
| 115 | + out.println("</body></html>\n"); |
| 116 | + } |
| 117 | +} |
| 118 | +// [END example] |
0 commit comments