Hey im trying to create a program which uses Floyd-Steinberg's dithering algorithm to produce a dithered version of a image.
the code for my program is below.
but I believe I am getting an error with with the rounding in the "calculateErr" multiplication with the diviser.
the image I am using to test is this cat one: Cat Image
and for some reason it ends up looking like this Dithered cat image
any solutions to what I am doing wrong would be greatly appreciated.
import java.awt.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Dithering extends Canvas {
public BufferedImage ditheredIMG = new BufferedImage(481,480,
BufferedImage.TYPE_BYTE_GRAY);
public void paint(Graphics g) {
BufferedImage i = null;
try {
i = displayImage(g);
} catch (IOException e) {
throw new RuntimeException(e);
}
getPixels(g, i);
g.drawImage(ditheredIMG,480,0,this);
}
public static void main(String[] args) {
Dithering d = new Dithering();
JFrame f =new JFrame();
f.add(d);
f.setSize( 960,481);
f.setVisible(true);
}
public BufferedImage displayImage(Graphics g) throws IOException {
final File file = new File("Cat.jpg");
final BufferedImage i = ImageIO.read(file);
g.drawImage(i, 0,0,this);
return i;
}
public void getPixels(Graphics g, BufferedImage i) {
for (int y = 1; y < i.getHeight()-1; y++){
for (int x = 1; x < i.getWidth()-1; x++) {
int pixelValue = i.getRGB(x, y);
int red = (pixelValue & 0x00ff0000) >> 16;
int green = (pixelValue & 0x0000ff00) >> 8;
int blue = pixelValue & 0x000000ff;
int newRed = quantisePixel(red);
int newGreen = quantisePixel(green);
int newBlue = quantisePixel(blue);
int newPixel = (newRed << 16) | (newGreen << 8) | newBlue;
ditheredIMG.setRGB(x+1,y, (int) (calculateErr(pixelValue, newPixel) * (7/16.0)));
ditheredIMG.setRGB(x-1,y+1, (int) (calculateErr(pixelValue, newPixel) * (3/16.0)));
ditheredIMG.setRGB(x,y+1, (int) (calculateErr(pixelValue, newPixel) * (5/16.0)));
ditheredIMG.setRGB(x+1,y+1, (int) (calculateErr(pixelValue, newPixel)* (1/16.0)));
}
}
}
public int calculateErr(int oldVal, int newVal){
return oldVal-newVal;
}
public static int quantisePixel(int val){
if(val > 127){
return 255;
} else{
return 0;
}
}
}
hey so here is the updated version but I am unsure if its working correctly if anyone can tell me if it is or is not that would be greatly appreciated. I've changed it so now its supposed to be updating the surrounding pixels correctly but from my eyes it looks to be the exact same as without updating the neighbours.
import java.awt.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import static java.lang.Math.abs;
public class Dithering extends Canvas {
public BufferedImage ditheredIMG = new BufferedImage(481,480,
BufferedImage.TYPE_BYTE_GRAY);
public void paint(Graphics g) {
BufferedImage i = null;
try {
i = displayImage(g);
} catch (IOException e) {
throw new RuntimeException(e);
}
getPixels(g, i);
g.drawImage(ditheredIMG,480,0,this);
}
public static void main(String[] args) {
Dithering d = new Dithering();
JFrame f =new JFrame();
f.add(d);
f.setSize( 960,481);
f.setVisible(true);
}
public BufferedImage displayImage(Graphics g) throws IOException {
final File file = new File("Cat.jpg");
final BufferedImage i = ImageIO.read(file);
g.drawImage(i, 0,0,this);
return i;
}
public void getPixels(Graphics g, BufferedImage i) {
for (int y = 1; y < i.getHeight()-1; y++){
for (int x = 1; x < i.getWidth()-1; x++) {
int pixelValue = i.getRGB(x, y);
int red = (pixelValue & 0x00ff0000) >> 16;
int green = (pixelValue & 0x0000ff00) >> 8;
int blue = pixelValue & 0x000000ff;
int newRed = quantisePixel(red);
int newGreen = quantisePixel(green);
int newBlue = quantisePixel(blue);
int newPixel = (newRed << 16) | (newGreen << 8) | newBlue;
ditheredIMG.setRGB(x,y,newPixel);
int newPixelValue = i.getRGB(x+1,y);
ditheredIMG.setRGB(x+1,y, (int) (newPixelValue + calculateErr(pixelValue, newPixel) * (7/16.0)));
newPixelValue = i.getRGB(x-1,y+1);
ditheredIMG.setRGB(x-1,y+1, (int) (newPixelValue + calculateErr(pixelValue, newPixel) * (3/16.0)));
newPixelValue = i.getRGB(x,y+1);
ditheredIMG.setRGB(x,y+1, (int) (newPixelValue + calculateErr(pixelValue, newPixel) * (5/16.0)));
newPixelValue = i.getRGB(x+1,y+1);
ditheredIMG.setRGB(x+1,y+1, (int) (newPixelValue + calculateErr(pixelValue, newPixel)* (1/16.0)));
}
}
}
public int calculateErr(int oldVal, int newVal){
return oldVal-newVal;
}
public static int quantisePixel(int val){
if(val > 127){
return 255;
} else{
return 0;
}
}
}
Related
I want to make the displayed image repainted for everytime i change the slider position.
I've already made the Every change of the Variable from JSlider is added to the pixel. But i just don't know how to repaint it.
package training;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.plaf.SliderUI;
import java.awt.Color;
import java.util.Arrays;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Training extends JPanel {
public BufferedImage image;
double maxw, maxh;
double w, h, ratio;
int warna, red, green, blue, abu, value;
int forT1, forT2;
int[][] bmpR;
int[][] bmpG;
int[][] bmpB;
int[][] alpha;
public Training () {
super();
try {
image = ImageIO.read(new File("src/training/V.jpg"));
}
catch (IOException e) {
//Not handled.
}
maxw = 750;
maxh = 600;
w = image.getWidth();
h = image.getHeight();
bmpR = new int[(int)w][(int)h];
bmpG = new int[(int)w][(int)h];
bmpB = new int[(int)w][(int)h];
if (w > h) {
if (w > maxw) {
ratio = maxw / w;
h = h * ratio; // Reset height to match scaled image
w = w * ratio;
}
}
if (w <= h) {
if (h > maxh) {
ratio = maxh / h;
w = w * ratio; // Reset height to match scaled image
h = h * ratio;
}
}
try {
for( int i = 0; i < w; i++ ) {
for( int j = 0; j < h; j++ ) {
Color c = new Color(image.getRGB(i, j));
bmpR [i][j] = c.getRed();
bmpG [i][j] = c.getGreen();
bmpB [i][j] = c.getBlue();
// alpha = c.getAlpha();
}
}
System.out.println(bmpB[40][40]);
}
catch (Exception e) {
System.out.println("Terjadi kesalahan saat mengambil data pixel");
e.printStackTrace();
return;
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Image i = image.getScaledInstance((int)w, (int)h,Image.SCALE_SMOOTH);
g.drawImage(i, 20, 20, null);
}
public static void main(String[] args) {
final Training ns = new Training();
System.out.println("User dir: " + System.getProperty("user.dir"));
JFrame f = new JFrame("Window");
JPanel p = new Training();
f.setSize(1100, 600);
p.setSize(750, 600);
f.add(p);
JSlider Temp = new JSlider(-50, 50, 0);
Temp.setMajorTickSpacing(10);
Temp.setMinorTickSpacing(1);
Temp.addChangeListener(new ChangeListener () {
public void stateChanged(ChangeEvent evt) {
JSlider Temp = (JSlider) evt.getSource();
if (Temp.getValueIsAdjusting()) {
ns.value = Temp.getValue();
for(ns.forT1 = 0; ns.forT1 < ns.w; ns.forT1++ ) {
for(ns.forT2 = 0; ns.forT2 < ns.h; ns.forT2++ ) {
ns.bmpB[ns.forT1][ns.forT2] = ns.bmpB[ns.forT1][ns.forT2] - ns.value;
if (ns.bmpB[ns.forT1][ns.forT2] > 255) {
ns.bmpB[ns.forT1][ns.forT2] = 255;
}
if (ns.bmpB[ns.forT1][ns.forT2] < 0) {
ns.bmpB[ns.forT1][ns.forT2] = 0;
}
ns.bmpR[ns.forT1][ns.forT2] = ns.bmpR[ns.forT1][ns.forT2] + ns.value;
if (ns.bmpR[ns.forT1][ns.forT2] > 255) {
ns.bmpR[ns.forT1][ns.forT2] = 255;
}
if (ns.bmpR[ns.forT1][ns.forT2] < 0) {
ns.bmpR[ns.forT1][ns.forT2] = 0;
}
}
}
}
ns.repaint();
}
});
f.add(Temp, BorderLayout.EAST);
f.setVisible(true);
}
}
Did i misplaced the ChangeListener or should i put paintComponent method after the change listener happens?
The random indentation of your code is making it hard to figure out exactly what's going on, but I don't see repaint() anywhere inside your ChangeListener. You need to use repaint() to trigger the repainting of your component.
Programming Language: Java
I am trying to convert certain pixels of this image to a different color to reveal a "Secret Message."
Most of the pixels are:
Red = 0, Green = 64, Blue = 0
The pixels that I want changed to R = 255, G = 255, B = 255 are:
Red = 5, Green = 64, Blue = 5
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Color;
public class ASSN2p2
{
private static int makeRGBColor(int red, int green, int blue)
{
int rgb = 0;
rgb = red*65536 + green*256 + blue;
return rgb;
}
private static int getRed(int pixel)
{
return (pixel >> 16) & 0xFF;
}
private static int getGreen(int pixel)
{
return (pixel >> 8) & 0xFF;
}
private static int getBlue(int pixel)
{
return (pixel) & 0xFF;
}
public static void main(String args[]) throws IOException
{
// int width = 300;
// int height = 200;
BufferedImage image = null;
File f = null;
try
{
f = new File("D:\\2016-2017\\Fall2016\\201_CSCE_Programming\\Assignment 2\\secretmessage.png");
image = ImageIO.read(f);
image = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
System.out.println("Reading Complete");
BufferedImage output = new BufferedImage(image.getWidth(), image.getHeight(),BufferedImage.TYPE_INT_ARGB);
//--------------------------------------
//-------------------------------------------
for (int y = 0; y < image.getHeight(); y++)
{
for (int x = 0; x < image.getWidth(); x++)
{
int pixel = image.getRGB(x, y);
int r,g,b;
r = getRed(pixel);
g = getGreen(pixel);
b = getBlue(pixel);
if ((r == 5) && (g == 64) && (b == 5))
{
r = 64;
b = 64;
b = 64;
image.setRGB(x,y,makeRGBColor(r,g,b));
} }
}
}
catch(IOException e)
{
System.out.println("Error: "+e);
}
// printPixelARGB(pixel);
// System.out.println("");
try
{
f = new File("D:\\2016-2017\\Fall2016\\201_CSCE_Programming\\Assignment 2\\output.png");
ImageIO.write(image, "png", f);
System.out.println("Writing Complete");
}
catch(IOException e)
{
System.out.println("Error: "+e);
}
}
}
This produces this:
I am having trouble understanding what has gone wrong!
Any assistance will be appreciated!
I believe that you're missing the alpha component of your new color. Change your code to:
private static int makeRGBColor(int red, int green, int blue) {
int rgb = 0xff000000 | (red << 16) | (green << 8) | blue;
return rgb;
}
to make sure that the alpha is set to full, otherwise your image will look completely transparent.
I've been trying to paint on canvas but i can't make it work, i can see the JFrame but it seems it doesn't call the paint method when the Mover() object is being added to it. This is the first time using canvas so i don't know what am i missing. Here is code:
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.io.File;
public class Move extends Canvas
{
private static int [][]imgRGB;
public Move()
{
try
{
BufferedImage hugeImage = ImageIO.read(new File("C:/Users/pc/Pictures/Nave.gif"));
imgRGB = convertToRGB(hugeImage);
}
catch(IOException e)
{
System.out.println(e);
}
}
public void Paint(Graphics g)
{
super.paint(g);
for(int i=0 ; i<imgRGB.length ; i++)
{
for(int j=0 ; j<imgRGB[i].length; j++)
{
g.setColor(new Color(imgRGB[i][j]));
g.drawLine(i,j,i,j);
}
}
}
private static int[][] convertToRGB(BufferedImage image) {
final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
final int width = image.getWidth();
final int height = image.getHeight();
final boolean hasAlphaChannel = image.getAlphaRaster() != null;
int[][] result = new int[height][width];
if (hasAlphaChannel) {
final int pixelLength = 4;
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
int argb = 0;
argb += (((int) pixels[pixel] & 0xff) << 24); // alpha
argb += ((int) pixels[pixel + 1] & 0xff); // blue
argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green
argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red
result[row][col] = argb;
col++;
if (col == width) {
col = 0;
row++;
}
}
} else {
final int pixelLength = 3;
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
int argb = 0;
argb += -16777216; // 255 alpha
argb += ((int) pixels[pixel] & 0xff); // blue
argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green
argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red
result[row][col] = argb;
col++;
if (col == width) {
col = 0;
row++;
}
}
}
return result;
}
public static void main(String[] args)
{
JFrame container = new JFrame("pixel");
container.add(new Move());
container.setSize(400,400);
container.setVisible(true);
container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public void Paint(Graphics g)
Method names are case sensitive. You should override paint(...).
Always use the #Override annotation and the compiler will notify you when you attempt to override a method that doesn't exist:
#Override
public void paint(Graphics g)
{
...
}
However, you should not be overriding Canvas in a Swing application.
Instead you should extend JPanel and then you should be overriding the paintComponent(...) method.
Read the section from the Swing tutorial on Custom Painting for more information and working examples.
Aloha,
i have trouble finding the error in my java code. In my opinion everything is fine and correct but the function is not executed correctly and I dont understand why. The function should detect the difference between the colors and calculate the arithmetic mean of them.
The resilt of it should be draw under the original picture. What did I miss, please help me?
package edge;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import javax.swing.JComponent;
import javax.swing.JFrame;
/**
*
* #author Alaska
*/
public class Edge extends JComponent {
final int W = 500;
final int H = 300;
Image m_TrgImg, m_SrcImg;
public Edge(JFrame father) {
try {
FileDialog diag = new FileDialog(father);
diag.setVisible(true);
m_SrcImg = getToolkit().getImage(diag.getDirectory() + diag.getFile()).getScaledInstance(W, H, Image.SCALE_SMOOTH);
MediaTracker mt = new MediaTracker(this);
mt.addImage(m_SrcImg, 0);
mt.waitForAll();
int[] srcPix = new int[W * H];
int[] trgPix = new int[W * H];
PixelGrabber grab = new PixelGrabber(m_SrcImg, 0, 0, W, H, srcPix, 0, W);
grab.getPixels();
MemoryImageSource imgProd = new MemoryImageSource(W, H, trgPix, 0, W);
m_TrgImg = createImage(imgProd);
detectEdges(srcPix, trgPix);
m_TrgImg.flush();
} catch (InterruptedException e) {
System.out.println(e);
}
}
#Override
public void paintComponent(Graphics g) {
g.drawImage(m_SrcImg, 0, 0, this);
g.drawImage(m_TrgImg, 0, H, this);
}
#Override
public Dimension getPreferredSize() {
return getMinimumSize();
}
#Override
public Dimension getMinimumSize() {
return new Dimension(W, H * 2);
}
private void detectEdges(int[] srcPix, int[] trgPix) {
for (int x = 0; x < W; ++x) {
for (int y = 0; y < H; ++y) {
trgPix[y * W + x] = compColor(srcPix, x, y);
}
}
}
private int getRed(int col) {
return (col >> 16) & 255;
}
private int getGreen(int col) {
return (col >> 8) & 255;
}
private int getBlue(int col) {
return col & 255;
}
private int compColor(int[] srcPix, int x, int y) {
int red = 0;
int green = 0;
int blue = 0;
int cnt = 0;
final int IDX = y * W + x;
final int RED = getRed(srcPix[IDX]);
final int GREEN = getGreen(srcPix[IDX]);
final int BLUE = getBlue(srcPix[IDX]);
for (int dx = -1; dx <= 1; ++dx) {
for (int dy = -1; dy <= 1; ++dy) {
if (dx != 0 || dy != 0) {
final int X = x + dx;
final int Y = y + dy;
final int LOCAL_IDX = Y * W + X;
if (0 <= X && X < W && 0 <= Y && Y < H) {
++cnt;
red += Math.abs(RED - getRed(srcPix[LOCAL_IDX]));
green += Math.abs(GREEN - getGreen(srcPix[LOCAL_IDX]));
blue += Math.abs(BLUE - getBlue(srcPix[LOCAL_IDX]));
}
}
}
}
return 0xff000000 | (255 - (red / cnt) << 16) | (255 - (green / cnt) << 8) | (255 - (blue / cnt));
}
public static void main(String[] args) throws Exception {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.getContentPane().add(new Edge(f));
f.pack();
f.setVisible(true);
}
}
You need to grab.grabPixels(), not grab.getPixels().
http://docs.oracle.com/javase/7/docs/api/java/awt/image/PixelGrabber.html
Also what trashgod said about Initial Threads. You need to create your GUI with SwingUtilities.invokeLater().
Edit
The method is executed correctly but you are getting all black values on the input because your pixel array contains only the initialized values which is 0.
CODE 1
/*
Java code for making the image grayscale, then binarizing it.
*/
import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.lang.Object;
import java.lang.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
public class lineremoval {
private static BufferedImage binarizedImage;
public static void main(String[] args) throws IOException {
File orignal_name = new File("photot.png");
binarizedImage = ImageIO.read(orignal_name);
ExtractBeltsBasedonCoveredHeight();
BufferedImage bmp = new BufferedImage(binarizedImage.getWidth(), binarizedImage.getHeight(),binarizedImage.getType());
for(int i=0; i<binarizedImage.getWidth(); i++) {
for(int j=0; j<binarizedImage.getHeight(); j++) {
int red;
red = new Color(binarizedImage.getRGB(i,j)).getRed();
int alpha = new Color(binarizedImage.getRGB(i,j)).getAlpha();
int newPixel;
newPixel = colorToRGB(alpha, red,red,red);
bmp.setRGB(i, j, newPixel);
}
}
writeImage(bmp,0);
}
public static int FindBottomOfLine(BufferedImage bitmap, int topOfLine)
{
int x=0;
boolean no_black_pixel;
no_black_pixel = false;
int to_match;
while (no_black_pixel == false)
{
topOfLine++;
int white=new Color(bitmap.getRGB(0,0)).getRed();
no_black_pixel = true;
for (x = 0; x < bitmap.getWidth() && topOfLine < bitmap.getHeight(); x++)
{
to_match = new Color(bitmap.getRGB(x,topOfLine)).getRed();
if (to_match!=white)
no_black_pixel = false;
}
}
return topOfLine - 1;
}
public static int ExtractBeltsBasedonCoveredHeight()
{
int y = 0;
int x = 0;
boolean line_present = true;
ArrayList<Integer> line_top = new ArrayList<Integer>(1000);
ArrayList<Integer> line_bottom = new ArrayList<Integer>(1000);
while (line_present)
{
x = 0;
y = FindNextLine(binarizedImage, y, x);
if (y == -1)
break;
if (y >= binarizedImage.getHeight())
{
line_present = false;
}
if (line_present)
{
line_top.add(y);
y = FindBottomOfLine(binarizedImage, y) + 1;
line_bottom.add(y);
}
}
return 1;
}
private static void writeImage(BufferedImage bmp,int number) throws IOException {
String strI = Integer.toString(number);
File file = new File("output"+strI+".png");
try {
ImageIO.write(bmp, "png", file);
}catch(IOException e) {
System.out.println("Not worked");
}
finally {
System.out.println("Works fine");
}
}
private static int colorToRGB(int alpha, int red, int green, int blue) {
int newPixel = 0;
newPixel += alpha;
newPixel = newPixel << 8;
newPixel += red; newPixel = newPixel << 8;
newPixel += green; newPixel = newPixel << 8;
newPixel += blue;
return newPixel;
}
public static int FindNextLine(BufferedImage bitmap, int y,int x)
{
if (y >= bitmap.getHeight())
return -1;
int white=new Color(bitmap.getRGB(0,0)).getRed();
int to_match = new Color(bitmap.getRGB(x,y)).getRed();
while (to_match==white)
{
x++;
if (x == bitmap.getWidth())
{
x = 0;
y++;
}
if (y >= bitmap.getHeight())
{
break;
}
to_match = new Color(bitmap.getRGB(x,y)).getRed();
}
return y < bitmap.getHeight() ? y : -1;
}
}
CODE 2
import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.lang.Object;
import java.lang.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
public class lineremoval {
private static BufferedImage binarizedImage;
public static void main(String[] args) throws IOException {
File orignal_name = new File("photot.png");
binarizedImage = ImageIO.read(orignal_name);
ExtractBeltsBasedonCoveredHeight();
}
public static int FindBottomOfLine(BufferedImage bitmap, int topOfLine)
{
int x=0;
boolean no_black_pixel;
no_black_pixel = false;
int to_match;
while (no_black_pixel == false)
{
topOfLine++;
int white=new Color(bitmap.getRGB(0,0)).getRed();
no_black_pixel = true;
for (x = 0; x < bitmap.getWidth() && topOfLine < bitmap.getHeight(); x++)
{
to_match = new Color(bitmap.getRGB(x,topOfLine)).getRed();
if (to_match!=white)
no_black_pixel = false;
}
}
return topOfLine - 1;
}
public static int ExtractBeltsBasedonCoveredHeight()
{
int y = 0;
int x = 0;
boolean line_present = true;
ArrayList<Integer> line_top = new ArrayList<Integer>(1000);
ArrayList<Integer> line_bottom = new ArrayList<Integer>(1000);
while (line_present)
{
x = 0;
y = FindNextLine(binarizedImage, y, x);
if (y == -1)
break;
if (y >= binarizedImage.getHeight())
{
line_present = false;
}
if (line_present)
{
line_top.add(y);
y = FindBottomOfLine(binarizedImage, y) + 1;
line_bottom.add(y);
}
}
BufferedImage bmp = new BufferedImage(binarizedImage.getWidth(), binarizedImage.getHeight(),binarizedImage.getType());
for(int i=0; i<binarizedImage.getWidth(); i++) {
for(int j=0; j<binarizedImage.getHeight(); j++) {
int red;
red = new Color(binarizedImage.getRGB(i,j)).getRed();
int alpha = new Color(binarizedImage.getRGB(i,j)).getAlpha();
int newPixel;
newPixel = colorToRGB(alpha, red,red,red);
bmp.setRGB(i, j, newPixel);
}
}
writeImage(bmp,0);
return 1;
}
private static void writeImage(BufferedImage bmp,int number) throws IOException {
String strI = Integer.toString(number);
File file = new File("output"+strI+".png");
try {
ImageIO.write(bmp, "png", file);
}catch(IOException e) {
System.out.println("Not worked");
}
finally {
System.out.println("Works fine");
}
}
private static int colorToRGB(int alpha, int red, int green, int blue) {
int newPixel = 0;
newPixel += alpha;
newPixel = newPixel << 8;
newPixel += red; newPixel = newPixel << 8;
newPixel += green; newPixel = newPixel << 8;
newPixel += blue;
return newPixel;
}
public static int FindNextLine(BufferedImage bitmap, int y,int x)
{
if (y >= bitmap.getHeight())
return -1;
int white=new Color(bitmap.getRGB(0,0)).getRed();
int to_match = new Color(bitmap.getRGB(x,y)).getRed();
while (to_match==white)
{
x++;
if (x == bitmap.getWidth())
{
x = 0;
y++;
}
if (y >= bitmap.getHeight())
{
break;
}
to_match = new Color(bitmap.getRGB(x,y)).getRed();
}
return y < bitmap.getHeight() ? y : -1;
}
}
I want to get x images from a bitmap Images containing x lines of paragraph.
each Image should contain one line from the paragraph.For this I have used function("ExtractBeltsBasedonCoveredHeight") to get two lists. In these lists ith element denotes the starting row no and end row no of ith line in paragraph (in list list_top and list_bottom).
After getting these values I am not able create Images from function ExtractBeltsBasedonCoveredHeight .
My code1 creates Images but code2 gives error both code are same the only difference is in code2 I have called writeImage from function "ExtractBeltsBasedonCoveredHeight" and in code1 I have called writeImage from main function
ERROR MESSAGE
lineremoval.java:86: error: unreported exception IOException; must be caught or declared to be thrown
writeImage(bmp,0);
^
1 error
The message is (as usual) telling you exactly what is wrong and how to fix it. You're calling code that has the potential to call an IOException, and when you do this you must either throw the exception or catch it in a try/catch block. Your best bet is to check out the exceptions tutorial to learn how to do both of these.