Ok so I'm designing a maze related game and I am now handling the GUI.
Considering it will be a NxN dimension, I have to resize the images (content of the labyrinth) according to the screen size, so it will remain untouched regardless of the maze size.
I used this piece of code:
public static BufferedImage resizeImage(Image image, int width, int height) {
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
return bufferedImage;
}
There is an issue though. The new resized image is now black (completely black) even though it is correctly resized (dimensions wise).
What am I doing wrong? Thanks in advance.
Here's how you can add in the code to draw a scaled version of your image on the new bitmap that you created:
public static BufferedImage resizeImage(Image image, int width, int height) {
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
// Increase quality if needed at the expense of speed
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
AffineTransform scaleTransform = AffineTransform.getScaleInstance(
width / (double) image.getWidth(null), height / (double) image.getHeight(null));
g.drawImage(image, scaleTransform, null);
// Release resources
g.dispose();
return bufferedImage;
}
Related
How can I create a black and white image using array of BLACK and WHITE pixels ?
int pixels[]= {-1, -16777216,-16777216,-16777216....}
Color w = new Color(Color.WHITE.getRGB());
int wi = w.getRGB();
Color b = new Color(Color.BLACK.getRGB());
int bi = b.getRGB();
I was trying to use this code but the result was only few black pixels.
public static BufferedImage getImageFromArray(int[] pixels, int width, int height) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
WritableRaster raster = (WritableRaster) image.getData();
raster.setDataElements(0, 0, width, height, pixels);
return image;
}
Not sure what exactly you are tying to create, but here are some nice examples:
http://www.lac.inpe.br/JIPCookbook/1100-create-bw.jsp
I am trying to use Tesseract OCR for scanning text in an image. However the background is dark and Tesseract is not able to scan thru for the text.
I need help with java code to clear the background gray color from the image.
I have tried below code, where i set the last parameter, Color, as "COLOR.WHITE" (suggested by someone on this forum but it did not help).
static public BufferedImage scaleImage(BufferedImage img, int width, int height,
Color background) {
int imgWidth = img.getWidth();
int imgHeight = img.getHeight();
if (imgWidth*height < imgHeight*width) {
width = imgWidth*height/imgHeight;
} else {
height = imgHeight*width/imgWidth;
}
BufferedImage newImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = newImage.createGraphics();
try {
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.setBackground(background);
g.clearRect(0, 0, width, height);
g.drawImage(img, 0, 0, width, height, null);
} finally {
g.dispose();
}
return newImage;
}
Thanks.
http://cafefiles.naver.net/20130606_179/rise1925_1370463189365uKV60_PNG/%C1%A6%B8%F1_%BE%F8%C0%BD.png
I tried to resize character images by bicubic algorithm in Java.
but as you can see it linked , connectivity of image has broken...
I just made code..
public BufferedImage scaleImage(BufferedImage img, int width, int height,
Color background) {
BufferedImage newImage = new BufferedImage(width, height,
BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g = newImage.createGraphics();
try {
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
//g.setBackground(background);
//g.setColor(Color.BLACK);
g.clearRect(0, 0, width, height);
g.drawImage(img, 0, 0, width, height, this);
} finally {
g.dispose();
}
return newImage;
}
what am i doing wrong?
Originaly I was just going to say use .getScaledInstance(), but I read an ardicle on good practices with the java Image and Buffered Image api.
https://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
And this is the sorce code that I think you need
public static BufferedImage scaleImage(BufferedImage img, int width, int height) {
BufferedImage newImage = new BufferedImage(width, height,img.getType());
Graphics2D g = newImage.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.clearRect(0, 0, width, height);
g.drawImage(img, 0, 0, width, height, null);
g.dispose();
return newImage;
}
You were setting the new re-sized image to be a binary (read as black and white ie 1 and 0) image. It is a good practice to always set a re-scaled image to the same type as the image the re-scaling is based off and to change its type with a conversion method.
I have this problem. I am using this code to rotate an image but the rotated image has black padding in its corners due to rotation.
How could I remove it?
public static BufferedImage rotate(BufferedImage img, int angle) {
rotate_checked = false;
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg =new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = dimg.createGraphics();
g.rotate(Math.toRadians(angle), w/2, h/2);
g.drawImage(img, null, 0, 0);
return dimg;
}
You need to create a transparent image:
BufferedImage buffer = gc.createCompatibleImage(height, width, Transparency.TRANSLUCENT);
where 'gc' is a Graphics2D object. You can also create one directly with new BufferedImage() of course, but this will give you the most efficient-to-use image for your particular graphics context.
Is there any faster way to achieve padding of pixels to a BufferedImage than drawing it centered on larger BufferedImage?
BufferedImage has a constructor where you get to specify a WriteableRaster.
Picking at the a default buffered image, storing each pixel in an int, it uses an IntegerInterleavedRaster.
The ColorModel you can use ColorModel.getRGBDefault().
int imageWidth = 638, imageHeight = 480;
int dataImageWidth = 640;
SampleModel sm = new SinglePixelPackedSampleModel(TYPE_INT, imageWidth, imageHeight, dataImageWidth, new int[] { 0xff0000, 0xff00, 0xff });
DataBuffer db = new DataBufferInt(dataImageWidth * imageHeight);
WritableRaster r = Raster.createWritableRaster(sm, db, new Point());
BufferedImage image = new BufferedImage(ColorModel.getRGBDefault(), r, false, null);
Notice the scanlineStride in SinglePixelPackedSampleModel (second last parameter).
Another much simpler approach is to use BufferedImage's getSubimage method.
BufferedImage fullImage = new BufferedImage(dataImageWidth, imageHeight);
BufferedImage subImage = fullImage.getSubimage(0, 0, imageWidth, imageHeight);
Create an ImageIcon using the BufferedImage and add the Icon to a JLabel. Then you can just add a Border to the label to get your desired padding.
To defer centering until rendering, I like this approach due to finnw, where this is a suitable component:
private BufferedImage image;
....
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.translate(this.getWidth() / 2, this.getHeight() / 2);
g2d.translate(-image.getWidth() / 2, -image.getHeight() / 2);
g2d.drawImage(image, 0, 0, null);
}