Java 创建BuffereImage时获取错误

Java 创建BuffereImage时获取错误,java,ocr,bufferedimage,Java,Ocr,Bufferedimage,代码1 这条信息(像往常一样)会准确地告诉你哪里出了问题,以及如何修复它。您调用的代码可能会调用IOException,执行此操作时,必须抛出异常或在try/catch块中捕获它。您最好的选择是查看异常教程,了解如何执行这两项操作。消息(与往常一样)会准确地告诉您错误所在以及如何修复。您调用的代码可能会调用IOException,执行此操作时,必须抛出异常或在try/catch块中捕获它。您最好的选择是查看异常教程,了解如何执行这两项操作。但是为什么我的code1可以工作。您是说我的code2会

代码1


这条信息(像往常一样)会准确地告诉你哪里出了问题,以及如何修复它。您调用的代码可能会调用IOException,执行此操作时,必须抛出异常或在try/catch块中捕获它。您最好的选择是查看异常教程,了解如何执行这两项操作。

消息(与往常一样)会准确地告诉您错误所在以及如何修复。您调用的代码可能会调用IOException,执行此操作时,必须抛出异常或在try/catch块中捕获它。您最好的选择是查看异常教程,了解如何执行这两项操作。

但是为什么我的code1可以工作。您是说我的code2会出错,但无法执行吗throw@stranger001:查看调用
writeImage(…)
方法的方法。两者都抛出异常吗?你确定吗?您是否在任何地方捕获它们?我的代码在writeImage函数中添加(尝试捕获并最终捕获)后工作,但我仍然不明白为什么我的code1不提供ERO,而code2提供ERO。@stranger001:再次,因为调用代码1中的
writeImage(…)
方法的方法引发IOException,而代码2中调用
writeImage(…)
的方法没有。我不知道如何用任何其他方式来解释这一点,但为什么我的代码1是有效的。你是说我的代码2给出了错误,但无法解释吗throw@stranger001:查看调用
writeImage(…)
方法的方法。两者都抛出异常吗?你确定吗?您是否在任何地方捕获它们?我的代码在writeImage函数中添加(尝试捕获并最终捕获)后工作,但我仍然不明白为什么我的code1不提供ERO,而code2提供ERO。@stranger001:再次,因为调用代码1中的
writeImage(…)
方法的方法引发IOException,而代码2中调用
writeImage(…)
的方法没有。我不知道该如何解释这一点,也不知道你能不能理解。
/*
   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;
    }

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

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