Convert each animated GIF frame to a separate BufferedImage - java

I want to be able to take an animated GIF as input, count the frames (and perhaps other metadata), and convert each to a BufferedImage.
How can I do this?

If you want all the frames to be the same size (for optimized GIFs) try something like this:
try {
String[] imageatt = new String[]{
"imageLeftPosition",
"imageTopPosition",
"imageWidth",
"imageHeight"
};
ImageReader reader = (ImageReader)ImageIO.getImageReadersByFormatName("gif").next();
ImageInputStream ciis = ImageIO.createImageInputStream(new File("house2.gif"));
reader.setInput(ciis, false);
int noi = reader.getNumImages(true);
BufferedImage master = null;
for (int i = 0; i < noi; i++) {
BufferedImage image = reader.read(i);
IIOMetadata metadata = reader.getImageMetadata(i);
Node tree = metadata.getAsTree("javax_imageio_gif_image_1.0");
NodeList children = tree.getChildNodes();
for (int j = 0; j < children.getLength(); j++) {
Node nodeItem = children.item(j);
if(nodeItem.getNodeName().equals("ImageDescriptor")){
Map<String, Integer> imageAttr = new HashMap<String, Integer>();
for (int k = 0; k < imageatt.length; k++) {
NamedNodeMap attr = nodeItem.getAttributes();
Node attnode = attr.getNamedItem(imageatt[k]);
imageAttr.put(imageatt[k], Integer.valueOf(attnode.getNodeValue()));
}
if(i==0){
master = new BufferedImage(imageAttr.get("imageWidth"), imageAttr.get("imageHeight"), BufferedImage.TYPE_INT_ARGB);
}
master.getGraphics().drawImage(image, imageAttr.get("imageLeftPosition"), imageAttr.get("imageTopPosition"), null);
}
}
ImageIO.write(master, "GIF", new File( i + ".gif"));
}
} catch (IOException e) {
e.printStackTrace();
}

None of the answers here are correct and suitable for animation. There are many problems in each solution so I wrote something that actually works with all gif files. For instance, this takes into account the actual width and height of the image instead of taking the width and height of the first frame assuming it will fill the entire canvas, no, unfortunately it's not that simple. Second, this doesn't leave any transparent pickles. Third, this takes into account disposal Methods. Fourth, this gives you delays between frames (* 10 if you want to use it in Thread.sleep()).
private ImageFrame[] readGif(InputStream stream) throws IOException{
ArrayList<ImageFrame> frames = new ArrayList<ImageFrame>(2);
ImageReader reader = (ImageReader) ImageIO.getImageReadersByFormatName("gif").next();
reader.setInput(ImageIO.createImageInputStream(stream));
int lastx = 0;
int lasty = 0;
int width = -1;
int height = -1;
IIOMetadata metadata = reader.getStreamMetadata();
Color backgroundColor = null;
if(metadata != null) {
IIOMetadataNode globalRoot = (IIOMetadataNode) metadata.getAsTree(metadata.getNativeMetadataFormatName());
NodeList globalColorTable = globalRoot.getElementsByTagName("GlobalColorTable");
NodeList globalScreeDescriptor = globalRoot.getElementsByTagName("LogicalScreenDescriptor");
if (globalScreeDescriptor != null && globalScreeDescriptor.getLength() > 0){
IIOMetadataNode screenDescriptor = (IIOMetadataNode) globalScreeDescriptor.item(0);
if (screenDescriptor != null){
width = Integer.parseInt(screenDescriptor.getAttribute("logicalScreenWidth"));
height = Integer.parseInt(screenDescriptor.getAttribute("logicalScreenHeight"));
}
}
if (globalColorTable != null && globalColorTable.getLength() > 0){
IIOMetadataNode colorTable = (IIOMetadataNode) globalColorTable.item(0);
if (colorTable != null) {
String bgIndex = colorTable.getAttribute("backgroundColorIndex");
IIOMetadataNode colorEntry = (IIOMetadataNode) colorTable.getFirstChild();
while (colorEntry != null) {
if (colorEntry.getAttribute("index").equals(bgIndex)) {
int red = Integer.parseInt(colorEntry.getAttribute("red"));
int green = Integer.parseInt(colorEntry.getAttribute("green"));
int blue = Integer.parseInt(colorEntry.getAttribute("blue"));
backgroundColor = new Color(red, green, blue);
break;
}
colorEntry = (IIOMetadataNode) colorEntry.getNextSibling();
}
}
}
}
BufferedImage master = null;
boolean hasBackround = false;
for (int frameIndex = 0;; frameIndex++) {
BufferedImage image;
try{
image = reader.read(frameIndex);
}catch (IndexOutOfBoundsException io){
break;
}
if (width == -1 || height == -1){
width = image.getWidth();
height = image.getHeight();
}
IIOMetadataNode root = (IIOMetadataNode) reader.getImageMetadata(frameIndex).getAsTree("javax_imageio_gif_image_1.0");
IIOMetadataNode gce = (IIOMetadataNode) root.getElementsByTagName("GraphicControlExtension").item(0);
NodeList children = root.getChildNodes();
int delay = Integer.valueOf(gce.getAttribute("delayTime"));
String disposal = gce.getAttribute("disposalMethod");
if (master == null){
master = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
master.createGraphics().setColor(backgroundColor);
master.createGraphics().fillRect(0, 0, master.getWidth(), master.getHeight());
hasBackround = image.getWidth() == width && image.getHeight() == height;
master.createGraphics().drawImage(image, 0, 0, null);
}else{
int x = 0;
int y = 0;
for (int nodeIndex = 0; nodeIndex < children.getLength(); nodeIndex++){
Node nodeItem = children.item(nodeIndex);
if (nodeItem.getNodeName().equals("ImageDescriptor")){
NamedNodeMap map = nodeItem.getAttributes();
x = Integer.valueOf(map.getNamedItem("imageLeftPosition").getNodeValue());
y = Integer.valueOf(map.getNamedItem("imageTopPosition").getNodeValue());
}
}
if (disposal.equals("restoreToPrevious")){
BufferedImage from = null;
for (int i = frameIndex - 1; i >= 0; i--){
if (!frames.get(i).getDisposal().equals("restoreToPrevious") || frameIndex == 0){
from = frames.get(i).getImage();
break;
}
}
{
ColorModel model = from.getColorModel();
boolean alpha = from.isAlphaPremultiplied();
WritableRaster raster = from.copyData(null);
master = new BufferedImage(model, raster, alpha, null);
}
}else if (disposal.equals("restoreToBackgroundColor") && backgroundColor != null){
if (!hasBackround || frameIndex > 1){
master.createGraphics().fillRect(lastx, lasty, frames.get(frameIndex - 1).getWidth(), frames.get(frameIndex - 1).getHeight());
}
}
master.createGraphics().drawImage(image, x, y, null);
lastx = x;
lasty = y;
}
{
BufferedImage copy;
{
ColorModel model = master.getColorModel();
boolean alpha = master.isAlphaPremultiplied();
WritableRaster raster = master.copyData(null);
copy = new BufferedImage(model, raster, alpha, null);
}
frames.add(new ImageFrame(copy, delay, disposal, image.getWidth(), image.getHeight()));
}
master.flush();
}
reader.dispose();
return frames.toArray(new ImageFrame[frames.size()]);
}
And the ImageFrame class:
import java.awt.image.BufferedImage;
public class ImageFrame {
private final int delay;
private final BufferedImage image;
private final String disposal;
private final int width, height;
public ImageFrame (BufferedImage image, int delay, String disposal, int width, int height){
this.image = image;
this.delay = delay;
this.disposal = disposal;
this.width = width;
this.height = height;
}
public ImageFrame (BufferedImage image){
this.image = image;
this.delay = -1;
this.disposal = null;
this.width = -1;
this.height = -1;
}
public BufferedImage getImage() {
return image;
}
public int getDelay() {
return delay;
}
public String getDisposal() {
return disposal;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}

Right, I have never done anything even slightly like this before, but a bit of Googling and fiddling in Java got me this:
public ArrayList<BufferedImage> getFrames(File gif) throws IOException{
ArrayList<BufferedImage> frames = new ArrayList<BufferedImage>();
ImageReader ir = new GIFImageReader(new GIFImageReaderSpi());
ir.setInput(ImageIO.createImageInputStream(gif));
for(int i = 0; i < ir.getNumImages(true); i++)
frames.add(ir.getRawImageType(i).createBufferedImage(ir.getWidth(i), ir.getHeight(i)));
return frames;
}
Edit: see Ansel Zandegran's modification to my answer.

To split an animated GIF into separate BufferedImage frames:
try {
ImageReader reader = ImageIO.getImageReadersByFormatName("gif").next();
File input = new File("input.gif");
ImageInputStream stream = ImageIO.createImageInputStream(input);
reader.setInput(stream);
int count = reader.getNumImages(true);
for (int index = 0; index < count; index++) {
BufferedImage frame = reader.read(index);
// Here you go
}
} catch (IOException ex) {
// An I/O problem has occurred
}

Alex's answer covers most cases, but it does have a couple of problems. It doesn't handle transparency correctly (at least according to common convention) and it is applying the current frame's disposal method to the previous frame which is incorrect. Here's a version that does handle those cases correctly:
private ImageFrame[] readGIF(ImageReader reader) throws IOException {
ArrayList<ImageFrame> frames = new ArrayList<ImageFrame>(2);
int width = -1;
int height = -1;
IIOMetadata metadata = reader.getStreamMetadata();
if (metadata != null) {
IIOMetadataNode globalRoot = (IIOMetadataNode) metadata.getAsTree(metadata.getNativeMetadataFormatName());
NodeList globalScreenDescriptor = globalRoot.getElementsByTagName("LogicalScreenDescriptor");
if (globalScreenDescriptor != null && globalScreenDescriptor.getLength() > 0) {
IIOMetadataNode screenDescriptor = (IIOMetadataNode) globalScreenDescriptor.item(0);
if (screenDescriptor != null) {
width = Integer.parseInt(screenDescriptor.getAttribute("logicalScreenWidth"));
height = Integer.parseInt(screenDescriptor.getAttribute("logicalScreenHeight"));
}
}
}
BufferedImage master = null;
Graphics2D masterGraphics = null;
for (int frameIndex = 0;; frameIndex++) {
BufferedImage image;
try {
image = reader.read(frameIndex);
} catch (IndexOutOfBoundsException io) {
break;
}
if (width == -1 || height == -1) {
width = image.getWidth();
height = image.getHeight();
}
IIOMetadataNode root = (IIOMetadataNode) reader.getImageMetadata(frameIndex).getAsTree("javax_imageio_gif_image_1.0");
IIOMetadataNode gce = (IIOMetadataNode) root.getElementsByTagName("GraphicControlExtension").item(0);
int delay = Integer.valueOf(gce.getAttribute("delayTime"));
String disposal = gce.getAttribute("disposalMethod");
int x = 0;
int y = 0;
if (master == null) {
master = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
masterGraphics = master.createGraphics();
masterGraphics.setBackground(new Color(0, 0, 0, 0));
} else {
NodeList children = root.getChildNodes();
for (int nodeIndex = 0; nodeIndex < children.getLength(); nodeIndex++) {
Node nodeItem = children.item(nodeIndex);
if (nodeItem.getNodeName().equals("ImageDescriptor")) {
NamedNodeMap map = nodeItem.getAttributes();
x = Integer.valueOf(map.getNamedItem("imageLeftPosition").getNodeValue());
y = Integer.valueOf(map.getNamedItem("imageTopPosition").getNodeValue());
}
}
}
masterGraphics.drawImage(image, x, y, null);
BufferedImage copy = new BufferedImage(master.getColorModel(), master.copyData(null), master.isAlphaPremultiplied(), null);
frames.add(new ImageFrame(copy, delay, disposal));
if (disposal.equals("restoreToPrevious")) {
BufferedImage from = null;
for (int i = frameIndex - 1; i >= 0; i--) {
if (!frames.get(i).getDisposal().equals("restoreToPrevious") || frameIndex == 0) {
from = frames.get(i).getImage();
break;
}
}
master = new BufferedImage(from.getColorModel(), from.copyData(null), from.isAlphaPremultiplied(), null);
masterGraphics = master.createGraphics();
masterGraphics.setBackground(new Color(0, 0, 0, 0));
} else if (disposal.equals("restoreToBackgroundColor")) {
masterGraphics.clearRect(x, y, image.getWidth(), image.getHeight());
}
}
reader.dispose();
return frames.toArray(new ImageFrame[frames.size()]);
}
private class ImageFrame {
private final int delay;
private final BufferedImage image;
private final String disposal;
public ImageFrame(BufferedImage image, int delay, String disposal) {
this.image = image;
this.delay = delay;
this.disposal = disposal;
}
public BufferedImage getImage() {
return image;
}
public int getDelay() {
return delay;
}
public String getDisposal() {
return disposal;
}
}
There is a good description of how GIF animations work in this ImageMagick tutorial.

I wrote a GIF image decoder on my own and released it under the Apache License 2.0 on GitHub. You can download it here: https://github.com/DhyanB/Open-Imaging. Example usage:
void example(final byte[] data) throws Exception {
final GifImage gif = GifDecoder .read(data);
final int width = gif.getWidth();
final int height = gif.getHeight();
final int background = gif.getBackgroundColor();
final int frameCount = gif.getFrameCount();
for (int i = 0; i < frameCount; i++) {
final BufferedImage img = gif.getFrame(i);
final int delay = gif.getDelay(i);
ImageIO.write(img, "png", new File(OUTPATH + "frame_" + i + ".png"));
}
}
The decoder supports GIF87a, GIF89a, animation, transparency and interlacing. Frames will have the width and height of the image itself and be placed on the correct position on the canvas. It respects frame transparency and disposal methods. Checkout the project description for more details such as the handling of background colors.
Additionally, the decoder doesn't suffer from this ImageIO bug: ArrayIndexOutOfBoundsException: 4096 while reading gif file.
I'd be happy to get some feedback. I've been testing with a representive set of images, however, some real field testing would be good.

Using c24w's solution, replace:
frames.add(ir.getRawImageType(i).createBufferedImage(ir.getWidth(i), ir.getHeight(i)));
With:
frames.add(ir.read(i));

Related

Humble Video take snapshot of given time

Hello, I'm using https://github.com/artclarke/humble-video to take a thumbnail from a video.
So far I have successfully managed to take a snapshot from a video at start with following method.
private static Path generateThumbnail(final Path videoFile)
throws InterruptedException, IOException {
final Demuxer demuxer = Demuxer.make();
demuxer.open(videoFile.toString(), null, false, true, null, null);
int streamIndex = -1;
Decoder videoDecoder = null;
String rotate = null;
final int numStreams = demuxer.getNumStreams();
for (int i = 0; i < numStreams; ++i) {
final DemuxerStream stream = demuxer.getStream(i);
final KeyValueBag metaData = stream.getMetaData();
final Decoder decoder = stream.getDecoder();
if (decoder != null
&& decoder.getCodecType() == MediaDescriptor.Type.MEDIA_VIDEO) {
videoDecoder = decoder;
streamIndex = i;
rotate = metaData.getValue("rotate", KeyValueBag.Flags.KVB_NONE);
break;
}
}
if (videoDecoder == null) {
throw new IOException("Not a valid video file");
}
videoDecoder.open(null, null);
final MediaPicture picture = MediaPicture.make(videoDecoder.getWidth(),
videoDecoder.getHeight(), videoDecoder.getPixelFormat());
final MediaPictureConverter converter = MediaPictureConverterFactory
.createConverter(MediaPictureConverterFactory.HUMBLE_BGR_24, picture);
final MediaPacket packet = MediaPacket.make();
BufferedImage image = null;
MUX : while (demuxer.read(packet) >= 0) {
if (packet.getStreamIndex() != streamIndex) {
continue;
}
int offset = 0;
int bytesRead = 0;
videoDecoder.decodeVideo(picture, packet, offset);
do {
bytesRead += videoDecoder.decode(picture, packet, offset);
if (picture.isComplete()) {
image = converter.toImage(null, picture);
break MUX;
}
offset += bytesRead;
} while (offset < packet.getSize());
}
if (image == null) {
throw new IOException("Unable to find a complete video frame");
}
if (rotate != null) {
final AffineTransform transform = new AffineTransform();
transform.translate(0.5 * image.getHeight(), 0.5 * image.getWidth());
transform.rotate(Math.toRadians(Double.parseDouble(rotate)));
transform.translate(-0.5 * image.getWidth(), -0.5 * image.getHeight());
final AffineTransformOp op = new AffineTransformOp(transform,
AffineTransformOp.TYPE_BILINEAR);
image = op.filter(image, null);
}
final Path target = videoFile.getParent()
.resolve(videoFile.getFileName() + ".thumb.jpg");
final double mul;
if (image.getWidth() > image.getHeight()) {
mul = 216 / (double) image.getWidth();
} else {
mul = 216 / (double) image.getHeight();
}
final int newW = (int) (image.getWidth() * mul);
final int newH = (int) (image.getHeight() * mul);
final Image thumbnailImage = image.getScaledInstance(newW, newH,
Image.SCALE_SMOOTH);
image = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_BGR);
final Graphics2D g2d = image.createGraphics();
g2d.drawImage(thumbnailImage, 0, 0, null);
g2d.dispose();
ImageIO.write(image, "jpeg", target.toFile());
return target.toAbsolutePath(); }
Now, what I want to do is take a snapshot after 2 seconds after the video starts, is it possible? I
have tried using the "Demuxer" -s seek method but no luck.
I have successfully made it with the following code
The method from the library
public int seek(int stream_index, long min_ts, long ts, long max_ts, int flags);
Parameters are
stream_index index of the stream which is used as time base reference
min_ts smallest acceptable timestamp
ts target timestamp
max_ts largest acceptable timestamp
My Implementation was
final int success = demuxer.seek(streamIndex, 0, 700, 99999999,VideoJNI.Demuxer_SEEK_FRAME_get());

How to Place Bitmaps Vertically

I've got some code from user xil3 where it merges the bitmaps horizontally. Does anyone know how I can make it to do it vertically instead?
public Bitmap combineImages(Bitmap c, Bitmap s, String loc) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom
Bitmap cs = null;
int width, height = 0;
if (c.getHeight() > s.getHeight()) {
width = c.getWidth() + s.getWidth();
height = c.getHeight();
} else {
width = c.getWidth() + s.getWidth();
height = s.getHeight();
}
cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
Canvas comboImage = new Canvas(cs);
comboImage.drawBitmap(c, 0f, 0f, null);
comboImage.drawBitmap(s, c.getHeight(), 0f, null);
// this is an extra bit I added, just incase you want to save the new image somewhere and then return the location
String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
OutputStream os = null;
try {
os = new FileOutputStream(loc + tmpImg);
cs.compress(Bitmap.CompressFormat.PNG, 100, os);
} catch (IOException e) {
Log.e("combineImages", "problem combining images", e);
}
return cs;
}
I ended up finding a new piece of code here which saves them vertically-
private Bitmap mergeMultiple(ArrayList<Bitmap> parts) {
int w = 0, h = 0;
for (int i = 0; i < parts.size(); i++) {
if (i < parts.size() - 1) {
w = parts.get(i).getWidth() > parts.get(i + 1).getWidth() ? parts.get(i).getWidth() : parts.get(i + 1).getWidth();
}
h += parts.get(i).getHeight();
}
Bitmap temp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(temp);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
int top = 0;
for (int i = 0; i < parts.size(); i++) {
top = (i == 0 ? 0 : top + parts.get(i).getHeight() + 100);
canvas.drawBitmap(parts.get(i), 0f, top,paint );
}
return temp;
}

drawImage error; no method found for drawImage

/**
* The purpose of this program is to make an image and turn it into a kaleidoscope
*
* #author (Danny Meijo)
* #version (07/27/2017)
*/
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
public class KaleidoscopeImage
{
private Picture canvas = null;
private Picture canvas2 = null;
private Picture pictureObj = null;
private Picture scaledPicture = null;
private Picture clippedPicture = null;
private Graphics g = null;
private Graphics g2 = null;
private Graphics gV2 = null;
private Graphics g2V2 = null;
private Graphics gV3 = null;
private Graphics g2V3 = null;
KaleidoscopeImage(Picture Canvas, Picture image, Picture Canvas2)
{
canvas = Canvas;
canvas2 = Canvas2;
pictureObj = image;
g = canvas.getGraphics();
g2 = (Graphics2D)g;
}
public Picture firstPanel()
{
g2.drawImage(pictureObj.getImage(), 0, canvas.getHeight() / 2, null);
Pixel bottomLeftPixel = null;
Pixel topRightPixel = null;
Color sourceColor1 = null;
for(int ty = 0, by = canvas.getHeight(); ty < canvas.getHeight() / 2; ty++, by--)
{
for(int lx = 0, rx = canvas.getWidth(); lx < canvas.getWidth() / 2; lx++, rx--)
{
bottomLeftPixel = canvas.getPixel(lx, by - 1);
sourceColor1 = bottomLeftPixel.getColor();
topRightPixel = canvas.getPixel(rx - 1, ty);
topRightPixel.setColor(sourceColor1);
}
}
Pixel sourcePixel = null;
Pixel targetPixel = null;
Color sourceColor2 = null;
Color targetColor = null;
for(int y = 0; y < canvas.getHeight() / 2; y++)
{
for(int lx = 0, rx = canvas.getWidth(); lx < canvas.getWidth() / 2; lx++, rx--)
{
sourcePixel = canvas.getPixel(rx - 1,y);
sourceColor2 = sourcePixel.getColor();
targetPixel = canvas2.getPixel(lx,y);
targetPixel.setColor(sourceColor2);
}
}
return canvas2;
}
public Picture secondPanel()
{
Pixel leftPixel = null;
Pixel rightPixel = null;
Color sourceColor = null;
for(int y = 0; y < canvas2.getHeight() / 2; y++)
{
for(int lx = 0, rx = canvas2.getWidth(); lx < canvas2.getWidth() / 2; lx++, rx--)
{
leftPixel = canvas2.getPixel(lx,y);
sourceColor = leftPixel.getColor();
rightPixel = canvas2.getPixel(rx - 1, y);
rightPixel.setColor(sourceColor);
}
}
return canvas2;
}
public Picture thirdPanel()
{
Pixel topPixel = null;
Pixel bottomPixel = null;
Color sourceColor = null;
for(int lx = 0, rx = canvas2.getWidth(); lx < canvas2.getWidth() / 2; lx++, rx--)
{
for(int ty = 0, by = canvas2.getHeight(); ty < canvas2.getHeight() / 2; ty++, by--)
{
topPixel = canvas2.getPixel(rx - 1, ty);
sourceColor = topPixel.getColor();
bottomPixel = canvas2.getPixel(rx - 1, by - 1);
bottomPixel.setColor(sourceColor);
}
}
return canvas2;
}
public Picture fourthPanel()
{
Pixel leftPixel = null;
Pixel rightPixel = null;
Color sourceColor = null;
for(int lx = 0, rx = canvas2.getWidth(); lx < canvas2.getWidth() / 2; lx++, rx--)
{
for(int ty = 0, by = canvas2.getHeight(); ty < canvas2.getHeight() / 2; ty++, by--)
{
leftPixel = canvas2.getPixel(rx - 1, by - 1);
sourceColor = leftPixel.getColor();
rightPixel = canvas2.getPixel(lx, by - 1);
rightPixel.setColor(sourceColor);
}
}
return canvas2;
}
public Picture scalePicture(double xFactor, double yFactor)
{
AffineTransform scaleTransform = new AffineTransform();
scaleTransform.scale(xFactor, yFactor);
scaledPicture = new Picture((int)(canvas2.getWidth() * xFactor), (int)(canvas2.getHeight() * yFactor));
gV2 = scaledPicture.getGraphics();
g2V2 = (Graphics2D)gV2;
g2V2.drawImage(canvas2.getImage(), scaleTransform, null);
return scaledPicture;
}
public Picture clipPicture(Color color)
{
Picture canvas3 = new Picture(canvas2.getWidth(), canvas2.getHeight());
Pixel sourcePixel = null;
Pixel targetPixel = null;
Color sourceColor = null;
Color targetColor = null;
for(int y = 0; y < canvas2.getHeight(); y++)
{
for(int x = 0; x < canvas.getWidth(); x++)
{
sourcePixel = canvas2.getPixel(x,y);
sourceColor = sourcePixel.getColor();
targetPixel = canvas3.getPixel(x,y);
targetPixel.setColor(sourceColor);
}
}
gV3 = canvas3.getGraphics();
g2V3 = (Graphics2D)gV3;
canvas3.setAllPixelsToAColor(color);
Ellipse2D.Double clip = new Ellipse2D.Double(0,0, canvas3.getHeight(), canvas3.getWidth());
g2V3.setClip(clip);
g2V3.drawImage(canvas2.getImage(), 0, 0, canvas3.getHeight(), canvas3.getWidth(), null);
return canvas3;
}
}
Sorry, this is my first post, and I am also very new to java, since I'm learning it over the summer. I was not sure how to cut it to just the parts that I need, but the problem I'm having is in the scalePicture method. I was copying what I saw in a demo program to scale the image down to 0.75x0.75. But, in my program, there is an error with the drawImage method, where as the demo progam had no error.
If you are curious this is the demo that I was copying:
import java.awt.geom.AffineTransform;
import java.awt.Graphics;
import java.awt.Graphics2D;
class ScalingDemo
{
private Picture originalPicture = null;
private Picture newPicture = null;
private Graphics g = null;
private Graphics2D g2 = null;
ScalingDemo(Picture pic)
{
originalPicture = pic;
}
public Picture scalePicture(double xFactor, double yFactor)
{
AffineTransform scaleTransform = new AffineTransform();
scaleTransform.scale(xFactor, yFactor);
newPicture = new Picture((int)(originalPicture.getWidth()*xFactor), (int)(originalPicture.getHeight()*yFactor));
g = newPicture.getGraphics();
g2 = (Graphics2D)g;
g2.drawImage(originalPicture.getImage(), scaleTransform, null);
return newPicture;
}
}
Looks like the error is at line:
g2V2.drawImage(canvas2.getImage(), scaleTransform, null); - there's no such method in java.awt.Graphics interface.
You should use method with another signature:
drawImage(Image img, int x, int y, ImageObserver observer) - see here

Auto crop an image white border from all four side in Java

I want to crop all four side white spaces.
the easiest way to auto crop the white border out of an image in java? Thanks in advance...
public class TrimWhite {
public class TrimWhite {
private BufferedImage img;
public TrimWhite(File input) {
try {
img = ImageIO.read(input);
} catch (IOException e) {
throw new RuntimeException( "Problem reading image", e );
}
}
public void trim() {
int width = getTrimmedWidth();
int height = getTrimmedHeight();
BufferedImage newImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = newImg.createGraphics();
g.drawImage( img, 0, 0, null );
img = newImg;
}
public void write(File f) {
try {
ImageIO.write(img, "bmp", f);
} catch (IOException e) {
throw new RuntimeException( "Problem writing image", e );
}
}
private int getTrimmedWidth() {
int height = this.img.getHeight();
int width = this.img.getWidth();
int trimmedWidth = 0;
for(int i = 0; i < height; i++) {
for(int j = width - 1; j >= 0; j--) {
if(img.getRGB(j, i) != Color.WHITE.getRGB() &&
j > trimmedWidth) {
trimmedWidth = j;
break;
}
}
}
return trimmedWidth;
}
private int getTrimmedHeight() {
int width = this.img.getWidth();
int height = this.img.getHeight();
int trimmedHeight = 0;
for(int i = 0; i < width; i++) {
for(int j = height - 1; j >= 0; j--) {
if(img.getRGB(i, j) != Color.WHITE.getRGB() &&
j > trimmedHeight) {
trimmedHeight = j;
break;
}
}
}
return trimmedHeight;
}
public static void main(String[] args) {
TrimWhite trim = new TrimWhite(new File("C:\\Users\\Administrator\\Desktop\\New folder (2)\\Untitled.png"));
trim.trim();
trim.write(new File("C:\\Users\\Administrator\\Desktop\\New folder (2)\\test.png"));
}
}
}
I want output must crop all four side white spaces please help!

How to replace color with another color in BufferedImage

So I have an image file that has a volcano on it. Everything else is 0xFFFF00FF (opaque magenta). I want to replace every pixel that contains that color with 0 (transparent). So far my method looks like this:
public static BufferedImage replace(BufferedImage image, int target, int preferred) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage newImage = new BufferedImage(width, height, image.getType());
int color;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
color = image.getRGB(i, j);
if (color == target) {
newImage.setRGB(i, j, preferred);
}
else {
newImage.setRGB(i, j, color);
}
}
}
return newImage;
}
This works fine but seems VERY slow. I have seen someone do this another way, but I have no idea what was going on. If someone knows a better way to do this, I would very much like to hear it.
While I haven't had a chance to test this thoroughly yet, using a LookupOp may well benefit from acceleration:
public class ColorMapper
extends LookupTable {
private final int[] from;
private final int[] to;
public ColorMapper(Color from,
Color to) {
super(0, 4);
this.from = new int[] {
from.getRed(),
from.getGreen(),
from.getBlue(),
from.getAlpha(),
};
this.to = new int[] {
to.getRed(),
to.getGreen(),
to.getBlue(),
to.getAlpha(),
};
}
#Override
public int[] lookupPixel(int[] src,
int[] dest) {
if (dest == null) {
dest = new int[src.length];
}
int[] newColor = (Arrays.equals(src, from) ? to : src);
System.arraycopy(newColor, 0, dest, 0, newColor.length);
return dest;
}
}
Using it is as easy as creating a LookupOp:
Color from = Color.decode("#ff00ff");
Color to = new Color(0, true);
BufferedImageOp lookup = new LookupOp(new ColorMapper(from, to), null);
BufferedImage convertedImage = lookup.filter(image, null);
To avoid iterating through the pixels, change the underlying ColorModel. Here is an example. Below is the snippet where the author takes the original BufferedImage and applies the new color model.
private static BufferedImage createImage() {
int width = 200;
int height = 200;
// Generate the source pixels for our image
// Lets just keep it to a simple blank image for now
byte[] pixels = new byte[width * height];
DataBuffer dataBuffer = new DataBufferByte(pixels, width*height, 0);
SampleModel sampleModel = new SinglePixelPackedSampleModel(
DataBuffer.TYPE_BYTE, width, height, new int[] {(byte)0xf});
WritableRaster raster = Raster.createWritableRaster(
sampleModel, dataBuffer, null);
return new BufferedImage(createColorModel(0), raster, false, null);
}
private static ColorModel createColorModel(int n) {
// Create a simple color model with all values mapping to
// a single shade of gray
// nb. this could be improved by reusing the byte arrays
byte[] r = new byte[16];
byte[] g = new byte[16];
byte[] b = new byte[16];
for (int i = 0; i < r.length; i++) {
r[i] = (byte) n;
g[i] = (byte) n;
b[i] = (byte) n;
}
return new IndexColorModel(4, 16, r, g, b);
}
private BufferedImage image = createImage();
image = new BufferedImage(createColorModel(e.getX()), image.getRaster(), false, null);
You can get the pixels[] array of the buffered image like so
int[] pixels = ((DataBufferInt) newImg().getDataBuffer()).getData();
and then set your colors like so
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
color = pixels[y * width + x];
if (color == target) {
pixels[y * width + x] = preferred;
}
else {
pixels[y * width + x] = color;
}
}
}
This is a slight speed up over using setRGB()

Categories