1

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

}

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

    }

我想从包含 x 行段落的位图图像中获取 x 图像。每个图像应包含段落中的一行。为此,我使用函数(“ExtractBeltsBasedonCoveredHeight”)来获取两个列表。在这些列表中,第 i 个元素表示段落中第 i 行的起始行号和结束行号(在列表 list_top 和 list_bottom 中)。

获得这些值后,我无法从函数 ExtractBeltsBasedonCoveredHeight 创建图像。

我的code1创建图像但code2给出错误两个代码都是相同的唯一区别是code2我从函数“ExtractBeltsBasedonCoveredHeight”调用了writeImage,在code1中我从主函数调用了writeImage

错误信息

lineremoval.java:86: error: unreported exception IOException; must be caught or declared to be thrown
                writeImage(bmp,0);
                          ^
1 error
4

1 回答 1

1

该消息(像往常一样)告诉您确切的问题以及如何解决它。您正在调用有可能调用 IOException 的代码,当您这样做时,您必须要么抛出异常,要么在 try/catch 块中捕获它。您最好的选择是查看异常教程以了解如何执行这两项操作。

于 2013-03-03T07:33:38.260 回答