how to rescale binary character image? - java

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.

Related

merged two bufferimages javafx

I am working in javafx BufferedImage.
I have two BufferedImages 1)Grayscaleimage and 2)rgbimage.
i want to merged this two images in that rgb image want to transperanet with black color.
How can i achirve this?
my code snippet is:
if (depth == 24)
{
bufferedImgCFM = getPixelArrayToBmpByteArray(
fbData.getBuf(), width, height, depth);
}
else
{
bufferedImgBMode = getPixelArrayToBmpByteArray(
fbData.getBuf(), width, height, depth);
}
finalimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) finalimage.getGraphics();
if (bufferedImgBMode != null) {
g.drawImage(bufferedImgBMode, 0, 0, width, height, null);
}
if (bufferedImgCFM != null) {
g.drawImage(bufferedImgCFM, 0, 0, width, height, null);
}
Image image = SwingFXUtils.toFXImage(finalimage, null);
getPixelArrayToBmpByteArray method is:
private BufferedImage getPixelArrayToBmpByteArray(byte[] pixelData, int width,
int height, int depth) throws Exception {
int[] pixels = byteToInt(pixelData);
BufferedImage image = null;
if (depth == 8) {
image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
} else if (depth == 24) {
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//int[][] result = new int[height][width];
}
WritableRaster raster = (WritableRaster) image.getData();
raster.setPixels(0, 0, width, height, pixels);
image.setData(raster);
return image;
}
When i merged this two images second image that is rgb image will hide first image. i want to transperant rgb image with black.

Image turns black after resizing Java Swing

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;
}

How to create image in Java

say in my program, i have this paint() method. my wish is to create an image of the rectangles that are drawn (with the for loop). I tried the method below and it did give me those rectangles (blue color), but the background is all black. When I run program without creating image, just drawing the rect on a JFrame, the background is white. How can i fix this. ?
public void paint(Graphics g) {
super.paint(g);
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g = Image.getGraphics(); <<<----- is this correct?
g.setColor(Color.blue);
for ( ..... ) {
g.fillRect(X , Y, width , height);
....
}
try {
ImageIO.write(image, "jpg", new File("CustomImage.jpg"));
}catch (IOException e) {
e.printStackTrace();
}
}
The background is black in your image because you are not giving any pixels a value except those in the rectangles. The BufferedImage is starting out with every pixel having RGB of (0, 0, 0), which is black. To give the entire image a white background, simply fill the entire rectangle that is the image with white.
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g = image.createGraphics(); // not sure on this line, but this seems more right
g.setColor(Color.white);
g.fillRect(0, 0, 100, 100); // give the whole image a white background
g.setColor(Color.blue);
for( ..... ){
g.fillRect(X , Y, width , height );
....
}
Note that my answer is about writing the image to a file with a white background, not about drawing to the JFrame with a black background. I'm not entirely sure which one you wanted.
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
Font font = new Font("Georgia", Font.BOLD, 18);
g2d.setFont(font);
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
GradientPaint gp = new GradientPaint(0, 0,
Color.red, 0, height/2, Color.black, true);
g2d.setPaint(gp);
g2d.fillRect(0, 0, width, height);
g2d.setColor(new Color(255, 153, 0));
Try this
public void paint(Graphics g) {
super.paint(g);
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
g = Image.createGraphics(); // it should be createGraphics
g.setBackground(Color.black);
g.setColor(Color.blue);
for( ..... ){
g.fillRect(X , Y, width , height );
....
}
try {
ImageIO.write(image, "jpg", new File("CustomImage.jpg"));
}catch (IOException e) {
e.printStackTrace();
}
}
It should be createGraphics.
http://docs.oracle.com/javase/tutorial/2d/images/drawonimage.html
..

Remove busy background from scanned image

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.

Black Padding with images in java?

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.

Categories