com.google.appengine.api.images.Image Java Examples

The following examples show how to use com.google.appengine.api.images.Image. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ImageUtil.java    From wmf2svg with Apache License 2.0 6 votes vote down vote up
public byte[] convert(byte[] image, String destType, boolean reverse) {
	if (destType == null) {
		throw new IllegalArgumentException("dest type is null.");
	} else {
		destType = destType.toLowerCase();
	}

	ImagesService.OutputEncoding encoding = null;
	if ("png".equals(destType)) {
		encoding = OutputEncoding.PNG;
	} else if ("jpeg".equals(destType)) {
		encoding = OutputEncoding.JPEG;
	} else {
		throw new UnsupportedOperationException("unsupported image encoding: " + destType);
	}

	ImagesService imagesService = ImagesServiceFactory.getImagesService();
	Image bmp = ImagesServiceFactory.makeImage(image);

	Transform t = (reverse) ? ImagesServiceFactory.makeVerticalFlip() : ImagesServiceFactory.makeCompositeTransform();
	return imagesService.applyTransform(t, bmp, encoding).getImageData();
}
 
Example #2
Source File: ImagesServiceTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
private void applyAndVerify(String fname, Transform transform, ChkType chkType, OutputEncoding outType) throws IOException {
    int expectedWidth = -1;
    int expectedHeight = -1;

    Image image = readImage(fname);
    if (chkType == ChkType.FLIP) {
        expectedWidth = image.getWidth();
        expectedHeight = image.getHeight();
    } else if (chkType == ChkType.ROTATE) {
        expectedWidth = image.getHeight();
        expectedHeight = image.getWidth();
    } else if (chkType == ChkType.CROP) {
        expectedWidth = image.getWidth() / 2;
        expectedHeight = image.getHeight() / 2;
    }
    Image transImg = imagesService.applyTransform(transform, image, outType);
    assertEquals(expectedWidth, transImg.getWidth());
    assertEquals(expectedHeight, transImg.getHeight());
}
 
Example #3
Source File: TransformationsTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testResizeWithCrop() throws IOException {
    int resizedWidth = 300;
    int resizedHeight = 200;
    Image originalImage = readImage(CAPEDWARF_PNG);
    Transform resize = ImagesServiceFactory.makeResize(resizedWidth, resizedHeight, 0.5f, 0.5f);
    Image resizedImage = imagesService.applyTransform(resize, originalImage);
    assertEquals(resizedWidth, resizedImage.getWidth());
    assertEquals(resizedHeight, resizedImage.getHeight());

    originalImage = readImage(CAPEDWARF_PNG);
    resize = ImagesServiceFactory.makeResize(resizedWidth, resizedHeight, 0.5, 0.5);
    resizedImage = imagesService.applyTransform(resize, originalImage);
    assertEquals(resizedWidth, resizedImage.getWidth());
    assertEquals(resizedHeight, resizedImage.getHeight());
}
 
Example #4
Source File: TransformationsTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testResizeAsync() throws IOException {
    InputSettings inputSettings = new InputSettings();
    OutputSettings outputSettings = new OutputSettings(OutputEncoding.PNG);

    Image originalImage = readImage(CAPEDWARF_PNG);
    Image resizedImage = waitOnFuture(imagesService.applyTransformAsync(ImagesServiceFactory.makeResize(400, 286), originalImage));
    assertEquals(400, resizedImage.getWidth());
    assertEquals(286, resizedImage.getHeight());

    originalImage = readImage(CAPEDWARF_PNG);
    resizedImage = waitOnFuture(imagesService.applyTransformAsync(ImagesServiceFactory.makeResize(300, 286), originalImage, outputSettings));
    assertEquals(300, resizedImage.getWidth());
    assertEquals(215, resizedImage.getHeight());

    originalImage = readImage(CAPEDWARF_PNG);
    resizedImage = waitOnFuture(imagesService.applyTransformAsync(ImagesServiceFactory.makeResize(300, 286), originalImage, inputSettings, outputSettings));
    assertEquals(300, resizedImage.getWidth());
    assertEquals(215, resizedImage.getHeight());

    originalImage = readImage(CAPEDWARF_PNG);
    resizedImage = waitOnFuture(imagesService.applyTransformAsync(ImagesServiceFactory.makeResize(300, 286), originalImage, OutputEncoding.JPEG));
    assertEquals(300, resizedImage.getWidth());
    assertEquals(215, resizedImage.getHeight());
}
 
Example #5
Source File: ImagesServiceTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testHistogram() throws IOException {
    int[][] expect = {{1, 63, 148, 23036}, {1408, 80, 79, 22192}, {1329, 82, 83, 22243}};
    Image image = readImage("jpgAttach.jpg");
    int[][] color = imagesService.histogram(image);
    assertEquals(3, color.length);
    for (int i = 0; i < color.length; i++) {
        assertEquals(256, color[i].length);
        assertEquals(expect[i][0], color[i][0]);
        assertEquals(expect[i][1], color[i][100]);
        assertEquals(expect[i][2], color[i][200]);
        assertEquals(expect[i][3], color[i][255]);
    }
}
 
Example #6
Source File: ImagesServiceTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testResize() throws IOException {
    for (String sfile : FNAMES) {
        for (int[] exptSize : NEW_SIZES) {
            Transform transform = ImagesServiceFactory.makeResize(exptSize[0], exptSize[1]);
            for (OutputEncoding encoding : ENCODES) {
                Image image = imagesService.applyTransform(transform, readImage(sfile), encoding);
                assertTrue((exptSize[0] == image.getWidth()) || (exptSize[1] == image.getHeight()));
            }
        }
    }
}
 
Example #7
Source File: IntegrationTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testHorizontalFlip() throws IOException {
    Image image = readImage("capedwarf.png");
    Transform horizontalFlip = ImagesServiceFactory.makeHorizontalFlip();

    Image flippedImage = imagesService.applyTransform(horizontalFlip, image);

    assertNotNull(flippedImage);
}
 
Example #8
Source File: TifImageTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
private void assertTransformation(Transform transform, String transformType) throws IOException {
    String expectedImage = "beach" + transformType + ".";
    for (OutputEncoding outType : OUTPUT_ENCODE) {
        String expectImageFile = expectedImage + outType.toString().toLowerCase();
        Image expected = readImage(expectImageFile);
        Image image = readImage(BEACH_TIF);
        Image transImg = imagesService.applyTransform(transform, image, outType);

        assertImages(transform, expected, transImg);
    }
}
 
Example #9
Source File: CompositeTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefault() throws IOException {
    // w - 200, h - 143
    Image originalImage = readImage(CAPEDWARF_PNG);
    Composite c1 = ImagesServiceFactory.makeComposite(originalImage, 0, 0, 1, Composite.Anchor.BOTTOM_LEFT);
    Image ci = imagesService.composite(newArrayList(c1), 200, 143, 0);
    assertImages(null, originalImage, ci);

}
 
Example #10
Source File: CompositeTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncoding() throws IOException {
    Image originalImage = readImage(CAPEDWARF_PNG);
    Composite c1 = ImagesServiceFactory.makeComposite(originalImage, 0, 0, 1, Composite.Anchor.BOTTOM_LEFT);
    Image ci = imagesService.composite(newArrayList(c1), 200, 143, 0, OutputEncoding.JPEG);
    assertImages(null, originalImage, ci);
}
 
Example #11
Source File: CompositeTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testSettings() throws IOException {
    Image originalImage = readImage(CAPEDWARF_PNG);
    Composite c1 = ImagesServiceFactory.makeComposite(originalImage, 0, 0, 1, Composite.Anchor.BOTTOM_LEFT);
    Image ci = imagesService.composite(newArrayList(c1), 200, 143, 0, new OutputSettings(OutputEncoding.JPEG));
    assertImages(null, originalImage, ci);
}
 
Example #12
Source File: CompositeTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransform() throws IOException {
    CompositeTransform transform = ImagesServiceFactory.makeCompositeTransform();
    transform.concatenate(ImagesServiceFactory.makeHorizontalFlip());
    transform.concatenate(ImagesServiceFactory.makeHorizontalFlip());
    Image originalImage = readImage(CAPEDWARF_PNG);
    Image result = imagesService.applyTransform(transform, originalImage);
    assertImages(transform, originalImage, result);

    transform = ImagesServiceFactory.makeCompositeTransform(newArrayList(ImagesServiceFactory.makeVerticalFlip(), ImagesServiceFactory.makeVerticalFlip()));
    originalImage = readImage(CAPEDWARF_PNG);
    result = imagesService.applyTransform(transform, originalImage);
    assertImages(transform, originalImage, result);
}
 
Example #13
Source File: TransformationsTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testResize() throws IOException {
    InputSettings inputSettings = new InputSettings();
    OutputSettings outputSettings = new OutputSettings(OutputEncoding.PNG);

    Image originalImage = readImage(CAPEDWARF_PNG);
    assertEquals(200, originalImage.getWidth());
    assertEquals(143, originalImage.getHeight());

    originalImage = readImage(CAPEDWARF_PNG);
    Image resizedImage = imagesService.applyTransform(ImagesServiceFactory.makeResize(400, 286), originalImage);
    assertEquals(400, resizedImage.getWidth());
    assertEquals(286, resizedImage.getHeight());

    originalImage = readImage(CAPEDWARF_PNG);
    resizedImage = waitOnFuture(imagesService.applyTransformAsync(ImagesServiceFactory.makeResize(400, 286), originalImage));
    assertEquals(400, resizedImage.getWidth());
    assertEquals(286, resizedImage.getHeight());

    originalImage = readImage(CAPEDWARF_PNG);
    resizedImage = imagesService.applyTransform(ImagesServiceFactory.makeResize(300, 286), originalImage, inputSettings, outputSettings);
    assertEquals(300, resizedImage.getWidth());
    assertEquals(215, resizedImage.getHeight());

    originalImage = readImage(CAPEDWARF_PNG);
    resizedImage = waitOnFuture(imagesService.applyTransformAsync(ImagesServiceFactory.makeResize(300, 286), originalImage, inputSettings, outputSettings));
    assertEquals(300, resizedImage.getWidth());
    assertEquals(215, resizedImage.getHeight());

    originalImage = readImage(CAPEDWARF_PNG);
    resizedImage = imagesService.applyTransform(ImagesServiceFactory.makeResize(400, 200), originalImage, outputSettings);
    assertEquals(280, resizedImage.getWidth());
    assertEquals(200, resizedImage.getHeight());
}
 
Example #14
Source File: ImagesServiceTestBase.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
protected void assertImages(Transform transform, Image expected, Image transImg) {
    ImageLifecycleEvent event = TestLifecycles.createImageLifecycleEvent(getClass(), transform, expected, transImg);
    TestLifecycles.before(event);
    Boolean result = event.result();

    if (result == null) {
        assertArrayEquals(expected.getImageData(), transImg.getImageData());
    } else {
        Assert.assertTrue("Images are not equal.", result);
    }
}
 
Example #15
Source File: TransformationsTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testResizeWithStretch() throws IOException {
    int resizedWidth = 300;
    int resizedHeight = 200;
    Image originalImage = readImage(CAPEDWARF_PNG);
    Transform resize = ImagesServiceFactory.makeResize(resizedWidth, resizedHeight, true);
    Image resizedImage = imagesService.applyTransform(resize, originalImage);

    assertEquals(resizedWidth, resizedImage.getWidth());
    assertEquals(resizedHeight, resizedImage.getHeight());
}
 
Example #16
Source File: MakeImageTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void blobKeyImage() throws IOException {
    Image i1 = ImagesServiceFactory.makeImageFromBlob(new BlobKey("gs/bucket/123"));
    Image i2 = ImagesServiceFactory.makeImageFromFilename("/gs/bucket/123");
    assertEquals(i1, i2); // should compare blob keys
}
 
Example #17
Source File: ImageLifecycleEventImpl.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
public Image getTransformed() {
    return transformed;
}
 
Example #18
Source File: MakeImageTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
private void assertMakeImageCanReadImage(byte[] imageData) {
    Image image = ImagesServiceFactory.makeImage(imageData);
    assertNotNull(image);
}
 
Example #19
Source File: ImagesServiceTestBase.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
protected Image readImage(String filename) throws IOException {
    return ImagesServiceFactory.makeImage(toByteArray(getImageStream(filename)));
}
 
Example #20
Source File: ImageLifecycleEventImpl.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
ImageLifecycleEventImpl(Class<?> caller, Transform context, Image expected, Image transformed) {
    super(caller, context);
    this.expected = expected;
    this.transformed = transformed;
}
 
Example #21
Source File: ImageLifecycleEventImpl.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
public Image getExpected() {
    return expected;
}
 
Example #22
Source File: GalleryServiceImpl.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
/**
 * when an app is published/updated, we need to move the image
 * that was temporarily uploaded into projects/projectid/image
 * into the gallery image
 * @param app gallery app
 */
private void setGalleryAppImage(GalleryApp app) {
  // best thing would be if GCS has a mv op, we can just do that.
  // don't think that is there, though, so for now read one and write to other
  // First, read the file from projects name
  boolean lockForRead = false;
  //String projectImageKey = app.getProjectImageKey();
  GallerySettings settings = loadGallerySettings();
  String projectImageKey = settings.getProjectImageKey(app.getProjectId());
  try {
    GcsService gcsService = GcsServiceFactory.createGcsService();
    //GcsFilename filename = new GcsFilename(GalleryApp.GALLERYBUCKET, projectImageKey);
    GcsFilename filename = new GcsFilename(settings.getBucket(), projectImageKey);
    GcsInputChannel readChannel = gcsService.openReadChannel(filename, 0);
    InputStream gcsis = Channels.newInputStream(readChannel);

    byte[] buffer = new byte[8000];
    int bytesRead = 0;
    ByteArrayOutputStream bao = new ByteArrayOutputStream();

    while ((bytesRead = gcsis.read(buffer)) != -1) {
      bao.write(buffer, 0, bytesRead);
    }
    // close the project image file
    readChannel.close();

    // if image is greater than 200 X 200, it will be scaled (200 X 200).
    // otherwise, it will be stored as origin.
    byte[] oldImageData = bao.toByteArray();
    byte[] newImageData;
    ImagesService imagesService = ImagesServiceFactory.getImagesService();
    Image oldImage = ImagesServiceFactory.makeImage(oldImageData);
    //if image size is too big, scale it to a smaller size.
    if(oldImage.getWidth() > 200 && oldImage.getHeight() > 200){
        Transform resize = ImagesServiceFactory.makeResize(200, 200);
        Image newImage = imagesService.applyTransform(resize, oldImage);
        newImageData = newImage.getImageData();
    }else{
        newImageData = oldImageData;
    }

    // set up the cloud file (options)
    // After publish, copy the /projects/projectId image into /apps/appId
    //String galleryKey = app.getImageKey();
    String galleryKey = settings.getImageKey(app.getGalleryAppId());

    //GcsFilename outfilename = new GcsFilename(GalleryApp.GALLERYBUCKET, galleryKey);
    GcsFilename outfilename = new GcsFilename(settings.getBucket(), galleryKey);
    GcsFileOptions options = new GcsFileOptions.Builder().mimeType("image/jpeg")
        .acl("public-read").cacheControl("no-cache").build();
    GcsOutputChannel writeChannel = gcsService.createOrReplace(outfilename, options);
    writeChannel.write(ByteBuffer.wrap(newImageData));

    // Now finalize
    writeChannel.close();

  } catch (IOException e) {
    // TODO Auto-generated catch block
    LOG.log(Level.INFO, "FAILED WRITING IMAGE TO GCS");
    e.printStackTrace();
  }
}
 
Example #23
Source File: ImagesServlet.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

  // [START original_image]
  // Read the image.jpg resource into a ByteBuffer.
  FileInputStream fileInputStream = new FileInputStream(new File("WEB-INF/image.jpg"));
  FileChannel fileChannel = fileInputStream.getChannel();
  ByteBuffer byteBuffer = ByteBuffer.allocate((int) fileChannel.size());
  fileChannel.read(byteBuffer);

  byte[] imageBytes = byteBuffer.array();

  // Write the original image to Cloud Storage
  gcsService.createOrReplace(
      new GcsFilename(bucket, "image.jpeg"),
      new GcsFileOptions.Builder().mimeType("image/jpeg").build(),
      ByteBuffer.wrap(imageBytes));
  // [END original_image]

  // [START resize]
  // Get an instance of the imagesService we can use to transform images.
  ImagesService imagesService = ImagesServiceFactory.getImagesService();

  // Make an image directly from a byte array, and transform it.
  Image image = ImagesServiceFactory.makeImage(imageBytes);
  Transform resize = ImagesServiceFactory.makeResize(100, 50);
  Image resizedImage = imagesService.applyTransform(resize, image);

  // Write the transformed image back to a Cloud Storage object.
  gcsService.createOrReplace(
      new GcsFilename(bucket, "resizedImage.jpeg"),
      new GcsFileOptions.Builder().mimeType("image/jpeg").build(),
      ByteBuffer.wrap(resizedImage.getImageData()));
  // [END resize]

  // [START rotate]
  // Make an image from a Cloud Storage object, and transform it.
  BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
  BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/" + bucket + "/image.jpeg");
  Image blobImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
  Transform rotate = ImagesServiceFactory.makeRotate(90);
  Image rotatedImage = imagesService.applyTransform(rotate, blobImage);

  // Write the transformed image back to a Cloud Storage object.
  gcsService.createOrReplace(
      new GcsFilename(bucket, "rotatedImage.jpeg"),
      new GcsFileOptions.Builder().mimeType("image/jpeg").build(),
      ByteBuffer.wrap(rotatedImage.getImageData()));
  // [END rotate]

  // [START servingUrl]
  // Create a fixed dedicated URL that points to the GCS hosted file
  ServingUrlOptions options =
      ServingUrlOptions.Builder.withGoogleStorageFileName("/gs/" + bucket + "/image.jpeg")
          .imageSize(150)
          .crop(true)
          .secureUrl(true);
  String url = imagesService.getServingUrl(options);
  // [END servingUrl]

  // Output some simple HTML to display the images we wrote to Cloud Storage
  // in the browser.
  PrintWriter out = resp.getWriter();
  out.println("<html><body>\n");
  out.println(
      "<img src='//storage.cloud.google.com/" + bucket + "/image.jpeg' alt='AppEngine logo' />");
  out.println(
      "<img src='//storage.cloud.google.com/"
          + bucket
          + "/resizedImage.jpeg' alt='AppEngine logo resized' />");
  out.println(
      "<img src='//storage.cloud.google.com/"
          + bucket
          + "/rotatedImage.jpeg' alt='AppEngine logo rotated' />");
  out.println("<img src='" + url + "' alt='Hosted logo' />");
  out.println("</body></html>\n");
}
 
Example #24
Source File: IntegrationTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testMakeImage() throws IOException {
    Image image = readImage("capedwarf.png");
    assertNotNull(image);
}
 
Example #25
Source File: CapeDwarfImageLifecycle.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
protected static BufferedImage getBufferedImage(Image image) {
    return convertToBufferedImage(image.getImageData());
}
 
Example #26
Source File: TestLifecycles.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
public static ImageLifecycleEvent createImageLifecycleEvent(Class<?> caller, Transform op, Image expected, Image transformed) {
    return new ImageLifecycleEventImpl(caller, op, expected, transformed);
}
 
Example #27
Source File: BlobEndpoint.java    From io2014-codelabs with Apache License 2.0 3 votes vote down vote up
/**
 * Retrieves the Image from Google Cloud Storage.
 *
 * @param bucketName  the bucket name of the Google Cloud Storage.
 * @param objectName  the object path to the image in the Google Cloud Storage.
 * @return
 */
private Image getImageFromGcs(String bucketName, String objectName) {
  BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
  BlobKey blobKey = blobstoreService.createGsBlobKey(
    "/gs/" + bucketName + "/" + objectName);
  return ImagesServiceFactory.makeImageFromBlob(blobKey);
}
 
Example #28
Source File: BlobEndpoint.java    From io2014-codelabs with Apache License 2.0 2 votes vote down vote up
/**
 * Retrieves the Image from Google Cloud Storage.
 *
 * @param bucketName  the bucket name of the Google Cloud Storage.
 * @param objectName  the object path to the image in the Google Cloud Storage.
 * @return
 */
private Image getImageFromGcs(String bucketName, String objectName) {
  // Implement the method to retrieve Image from Google Cloud Storage.
  return null;
}
 
Example #29
Source File: ImageTransformer.java    From io2014-codelabs with Apache License 2.0 2 votes vote down vote up
/**
 * Applies some transformations to the old image and returns the transformed image.
 *
 * @param oldImage
 * @return
 */
public Image transform(Image oldImage);
 
Example #30
Source File: ImageLifecycleEvent.java    From appengine-tck with Apache License 2.0 2 votes vote down vote up
/**
 * Get expected image.
 *
 * @return the expected image
 */
Image getExpected();