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

The following examples show how to use com.google.appengine.api.images.Transform. 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: ImagesServiceTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testRotate() throws IOException {
    ChkType chkType;
    for (int dg : DEGREES) {
        Transform transform = ImagesServiceFactory.makeRotate(dg);
        if ((dg == 90) || (dg == 270)) {
            chkType = ChkType.ROTATE;
        } else {
            chkType = ChkType.FLIP;
        }
        for (String sfile : FNAMES) {
            for (OutputEncoding encoding : ENCODES) {
                applyAndVerify(sfile, transform, chkType, encoding);
            }
        }
    }
}
 
Example #4
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 #5
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 #6
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 #7
Source File: ImagesServiceTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testCrop() throws IOException {
    ChkType chkType = ChkType.CROP;
    String cropLeftX = "0.0";
    String cropTopY = "0.0";
    String cropRightX = "0.5";
    String cropBottomY = "0.5";
    Transform transform = ImagesServiceFactory.makeCrop(new Float(cropLeftX), new Float(cropTopY), new Float(cropRightX), new Float(cropBottomY));
    for (OutputEncoding encoding : ENCODES) {
        applyAndVerify("pngAttach.png", transform, chkType, encoding);
    }
}
 
Example #8
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 #9
Source File: ImagesServiceTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testVerticalFlip() throws IOException {
    ChkType chkType = ChkType.FLIP;
    Transform transform = ImagesServiceFactory.makeVerticalFlip();
    for (String sfile : FNAMES) {
        for (OutputEncoding encoding : ENCODES) {
            applyAndVerify(sfile, transform, chkType, encoding);
        }
    }
}
 
Example #10
Source File: ImagesServiceTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testHorizontalFlip() throws IOException {
    ChkType chkType = ChkType.FLIP;
    Transform transform = ImagesServiceFactory.makeHorizontalFlip();
    for (String sfile : FNAMES) {
        for (OutputEncoding encoding : ENCODES) {
            applyAndVerify(sfile, transform, chkType, encoding);
        }
    }
}
 
Example #11
Source File: ImagesServiceTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testFeelLucky() throws IOException {
    // I'm Feeling Lucky is not available in dev_appserver, CapeDwarf
    assumeEnvironment(Environment.APPSPOT);

    Transform transform = ImagesServiceFactory.makeImFeelingLucky();
    for (String sfile : FNAMES) {
        for (OutputEncoding encoding : ENCODES) {
            applyAndVerify(sfile, transform, ChkType.FLIP, encoding);
        }
    }
}
 
Example #12
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 #13
Source File: TifImageTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testCrop() throws IOException {
    assumeEnvironment(Environment.APPSPOT, Environment.CAPEDWARF);
    double cropLeftX = 0.0;
    double cropTopY = 0.0;
    double cropRightX = 0.5;
    double cropBottomY = 0.5;
    Transform transform = ImagesServiceFactory.makeCrop(
        cropLeftX,
        cropTopY,
        cropRightX,
        cropBottomY
    );
    assertTransformation(transform, "Crop");
}
 
Example #14
Source File: TifImageTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testRotate() throws IOException {
    assumeEnvironment(Environment.APPSPOT, Environment.CAPEDWARF);
    int rotageDegree = 90;
    Transform transform = ImagesServiceFactory.makeRotate(rotageDegree);
    assertTransformation(transform, "Rotate");
}
 
Example #15
Source File: TifImageTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testResize() throws IOException {
    assumeEnvironment(Environment.APPSPOT, Environment.CAPEDWARF);
    int resizeWidth = 150;
    int resizeHeigh = 150;
    Transform transform = ImagesServiceFactory.makeResize(resizeWidth, resizeHeigh);
    assertTransformation(transform, "Resize");
}
 
Example #16
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 #17
Source File: CapeDwarfImageLifecycle.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected <V> V getFieldValue(Transform op, Field field) {
    try {
        return (V) field.get(op);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
 
Example #18
Source File: TifImageTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testFeelingLucky() throws IOException {
    assumeEnvironment(Environment.APPSPOT);
    Transform transform = ImagesServiceFactory.makeImFeelingLucky();
    assertTransformation(transform, "ImFeelingLucky");
}
 
Example #19
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 #20
Source File: TifImageTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testVerticalFlip() throws IOException {
    assumeEnvironment(Environment.APPSPOT, Environment.CAPEDWARF);
    Transform transform = ImagesServiceFactory.makeVerticalFlip();
    assertTransformation(transform, "VerticalFlip");
}
 
Example #21
Source File: TifImageTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void testHorizontalFlip() throws IOException {
    assumeEnvironment(Environment.APPSPOT, Environment.CAPEDWARF);
    Transform transform = ImagesServiceFactory.makeHorizontalFlip();
    assertTransformation(transform, "HorizontalFlip");
}
 
Example #22
Source File: CapeDwarfImageLifecycle.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
protected <V> V getFieldValue(Transform op, String fieldName) {
    return (V) getFieldValue(op, getAccessibleField(op.getClass(), fieldName));
}
 
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: 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 #25
Source File: ImageLifecycleEventImpl.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
public Transform getOp() {
    return getContext();
}
 
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: ImageLifecycleEvent.java    From appengine-tck with Apache License 2.0 2 votes vote down vote up
/**
 * Get transform op.
 *
 * @return the transform op
 */
Transform getOp();