-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ImageScaler to make smaller animated gifs possible
- Loading branch information
Showing
5 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
...in/java/com/github/rtyley/android/screenshot/paparazzo/processors/AnimatedGifCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
.../src/main/java/com/github/rtyley/android/screenshot/paparazzo/processors/ImageScaler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.github.rtyley.android.screenshot.paparazzo.processors; | ||
|
||
import com.github.rtyley.android.screenshot.paparazzo.processors.util.Dimensions; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.util.Map; | ||
|
||
import static com.github.rtyley.android.screenshot.paparazzo.processors.util.Images.imageToBufferedImage; | ||
import static com.github.rtyley.android.screenshot.paparazzo.processors.util.Images.scaleTo; | ||
|
||
public class ImageScaler implements ScreenshotProcessor { | ||
|
||
private final ScreenshotProcessor delegate; | ||
private final Dimensions maxBounds; | ||
|
||
public ImageScaler(ScreenshotProcessor delegate, Dimensions maxBounds) { | ||
this.delegate = delegate; | ||
this.maxBounds = maxBounds; | ||
} | ||
|
||
@Override | ||
public void process(BufferedImage image, Map<String, String> request) { | ||
delegate.process(ensureWithinBounds(image), request); | ||
} | ||
|
||
private BufferedImage ensureWithinBounds(BufferedImage image) { | ||
Dimensions imageDimensions = new Dimensions(image.getWidth(), image.getHeight()); | ||
|
||
if (maxBounds.contains(imageDimensions)) { | ||
return image; | ||
} | ||
|
||
return imageToBufferedImage(scaleTo(imageDimensions.scaledPreservingAspectRatioToFitWithin(maxBounds), image)); | ||
} | ||
|
||
@Override | ||
public void finish() { | ||
delegate.finish(); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
.../main/java/com/github/rtyley/android/screenshot/paparazzo/processors/util/Dimensions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.github.rtyley.android.screenshot.paparazzo.processors.util; | ||
|
||
|
||
import static java.lang.Math.round; | ||
|
||
public class Dimensions { | ||
|
||
public final int width, height; | ||
public final double aspectRatio; | ||
|
||
public Dimensions(int width, int height) { | ||
this.width = width; | ||
this.height = height; | ||
aspectRatio = (double) width / height; | ||
} | ||
|
||
public static Dimensions square(int size) { | ||
return new Dimensions(size, size); | ||
} | ||
|
||
public Dimensions scaledPreservingAspectRatioToFitWithin(Dimensions bounds) { | ||
return bounds.aspectRatio > aspectRatio ? scaleToHeight(bounds.height) : scaleToWidth(bounds.width); | ||
} | ||
|
||
private Dimensions scaleToWidth(int targetWidth) { | ||
return new Dimensions(targetWidth, (int) round(targetWidth / aspectRatio)); | ||
} | ||
|
||
private Dimensions scaleToHeight(int targetHeight) { | ||
return new Dimensions((int) round(targetHeight * aspectRatio), targetHeight); | ||
} | ||
|
||
public boolean contains(int w, int h) { | ||
return width >= w && height >= h; | ||
} | ||
|
||
public boolean contains(Dimensions otherDimensions) { | ||
return contains(otherDimensions.width, otherDimensions.height); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getClass().getSimpleName() + "[" + toSimpleString() + "]"; | ||
} | ||
|
||
public String toSimpleString() { | ||
return "" + width + "x" + height; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
.../src/main/java/com/github/rtyley/android/screenshot/paparazzo/processors/util/Images.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.github.rtyley.android.screenshot.paparazzo.processors.util; | ||
|
||
import java.awt.*; | ||
import java.awt.image.BufferedImage; | ||
|
||
import static java.awt.Image.SCALE_SMOOTH; | ||
import static java.awt.image.BufferedImage.TYPE_INT_RGB; | ||
|
||
public class Images { | ||
|
||
public static BufferedImage imageToBufferedImage(Image image) { | ||
if (image instanceof BufferedImage) { | ||
return (BufferedImage) image; | ||
} | ||
BufferedImage target = newImage(image.getWidth(null), image.getHeight(null)); | ||
Graphics2D graphics = target.createGraphics(); | ||
graphics.drawImage(image, 0, 0, null); | ||
graphics.dispose(); | ||
return target; | ||
} | ||
|
||
public static BufferedImage newImage(int width, int height) { | ||
return new BufferedImage(width, height, TYPE_INT_RGB); | ||
} | ||
|
||
public static Image scaleTo(Dimensions scaledDimensions, BufferedImage image) { | ||
return image.getScaledInstance(scaledDimensions.width, scaledDimensions.height, SCALE_SMOOTH); | ||
} | ||
|
||
} |