How do you clone a BufferedImage
Something like this?
static BufferedImage deepCopy(BufferedImage bi) {
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.copyData(null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
How to copy an Image/BufferedImage
Take a look at the top answer of this question and see if it fits your situation:
How do you clone a BufferedImage
How to copy bufferedImage
Of course! You have to go and paint the image at the outskirts. Please use this
g.drawImage(this.createdImage, 0, 0, this.createdImage.getWidth(),this.createdImage.getHeight(),null);
How do you clone a BufferedImage
Something like this?
static BufferedImage deepCopy(BufferedImage bi) {
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.copyData(null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
How to clone Image?
You can draw to a buffered image, so make a blank bufferedImage, create a graphics context from it, and draw your original image to it.
BufferedImage copyOfImage =
new BufferedImage(widthOfImage, heightOfImage, BufferedImage.TYPE_INT_RGB);
Graphics g = copyOfImage.createGraphics();
g.drawImage(originalImage, 0, 0, null);
How to copy image data into a subclass of BufferedImage?
This is in error:
public void copyImage(Image image) {
if (image != null) {
this.width = image.getWidth(null);
this.height = image.getWidth(null);
BufferedImage bi = (BufferedImage) image;
Graphics g = getGraphics();
g.drawImage(bi, 0, 0, width, height, null);
}
}
Your main problems are:
- You appear to be trying to change the intrinsic width and height of the original image, the
this
image, and you shouldn't do this, not this way - You are assigning the parameter image's width to the
this.height
field withthis.height = image.getWidth(null);
Other issues:
- You're not conserving resources
- You're making a dangerous and unnecessary cast
and it should be
public void copyImage(Image image) {
if (image != null) {
// don't change the width/height of your original image
int width = image.getWidth(null);
// int height = image.getWidth(null);
int height = image.getHeight(null); // *** Note change ***
// BufferedImage bi = (BufferedImage) image; // *** no need ***
Graphics g = getGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.dispose(); // save resources
}
}
Test code using a MCVE showing proof of concept:
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class TestImage {
public static final String SOMME_PATH = "https://upload.wikimedia.org/"
+ "wikipedia/commons/thumb/f/fa/Cheshire_Regiment_trench_Somme_1916.jpg"
+ "/1024px-Cheshire_Regiment_trench_Somme_1916.jpg";
public static final String BATTLE_PATH = "https://upload.wikimedia.org/wikipedia/"
+ "commons/1/13/K%C3%A4mpfe_auf_dem_Doberdo.JPG";
public static void main(String[] args) {
int imgW = 1000;
int imgH = 700;
MyImage myImage = new MyImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB);
BufferedImage sommeTrench = null;
BufferedImage battleOfDoberdò = null;
try {
URL url = new URL(SOMME_PATH);
sommeTrench = ImageIO.read(url);
url = new URL(BATTLE_PATH);
battleOfDoberdò = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
Icon icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "Original MyImage", JOptionPane.PLAIN_MESSAGE);
myImage.copyImage(sommeTrench);
icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "MyImage with Somme Trench", JOptionPane.PLAIN_MESSAGE);
myImage.copyImage(battleOfDoberdò);
icon = new ImageIcon(myImage);
JOptionPane.showMessageDialog(null, icon, "MyImage with Battle Of Doberdò", JOptionPane.PLAIN_MESSAGE);
}
}
class MyImage extends BufferedImage {
public MyImage(int width, int height, int imageType) {
super(width, height, imageType);
}
public void copyImage(Image image) {
if (image != null) {
int width = image.getWidth(null);
int height = image.getHeight(null); // *** Note change ***
Graphics g = getGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.dispose(); // save resources
}
}
}
If you run this code you will see 3 images displaying as ImageIcons within 3 JOptionPanes, the first the original blank MyImage object, and then after 2 images from World War I have been copied into the original image.
Java BufferedImage: How do i copy an image properly n times by accessing the pixel - array?
The code you have uses scanline=8 as last parameter to setRGB and also wrong logic in copyNTimes which causes your stripped effect. If you want 8x8 pixel image repeating into 64x64 pixel image as 8x8 blocks either replace your setRGB call with this to repeat the small image into the larger one:
for (int x = 0 ; x < 64 ; x += 8)
for (int y = 0 ; y < 64 ; y += 8)
copiedNTimes.setRGB(x, y, 8, 8, smallPixels, 0, 8);
Or replace your setRGB call with this to build the larger int[] first and apply it in one step:
copiedNTimes.setRGB(0, 0, 64, 64, copied, 0, 64);
static int[] copyNTimes(int[] small, int[] big){
for(int x = 0 ; x < big.length; x++){
big[x] = small[8 * ((x / 64) % 8) + (x % 8)];
}
return big;
}
Related Topics
How to Annotate MySQL Autoincrement Field with JPA Annotations
Jdbc Class.Forname VS Drivermanager.Registerdriver
How to Make My String Comparison Case-Insensitive
How to Launch a Completely Independent Process from a Java Program
How to Map a Postgresql Array with Hibernate
Maven Error: Could Not Find or Load Main Class Org.Codehaus.Plexus.Classworlds.Launcher.Launcher
Precise Definition of "Functional Interface" in Java 8
How to Detect the Presence of Url in a String
Killing Thread After Some Specified Time Limit in Java
How to Perform String Diffs in Java
How to Select/Get Drop Down Option in Selenium 2