Java Tomcat(8.5.32)服务器中的OpenCV库无法执行

Java Tomcat(8.5.32)服务器中的OpenCV库无法执行,java,opencv,error-handling,tesseract,tomcat8,Java,Opencv,Error Handling,Tesseract,Tomcat8,图像处理代码设置时面临的问题。尽管做了所有的代码更改和不同的方法来面对这个问题 使用的库–OpenCV–3.4.2 使用的罐子–opencv-3.4.2-0 tess4j-3.4.8 在pom.xml中添加的行 <!-- https://mvnrepository.com/artifact/org.openpnp/opencv --> <dependency> <groupId>org.openpnp</groupId>

图像处理代码设置时面临的问题。尽管做了所有的代码更改和不同的方法来面对这个问题

使用的库–OpenCV–3.4.2 使用的罐子–opencv-3.4.2-0 tess4j-3.4.8

在pom.xml中添加的行

<!-- https://mvnrepository.com/artifact/org.openpnp/opencv -->
    <dependency>
        <groupId>org.openpnp</groupId>
        <artifactId>opencv</artifactId>
        <version>3.4.2-0</version>
    </dependency>

<!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j -->
    <dependency>
        <groupId>net.sourceforge.tess4j</groupId>
        <artifactId>tess4j</artifactId>
        <version>3.4.8</version>
    </dependency>

org.openpnp
opencv
3.4.2-0
net.sourceforge.4j
tess4j
3.4.8
OpenCV安装步骤:

  • 从官方网站下载opencv.exe
  • 运行opencv.exe,它将创建一个opencv文件夹
  • 我们现在有了opencv库,可以用于eclipse
  • Tesseract安装步骤:

  • 从官方链接下载tess4j.zip文件
  • 下载后解压缩zip文件夹
  • 提供tess4j文件夹的路径
  • 以下是我们在eclipse中为安装程序执行的步骤:

  • 通过提供从构建路径设置到openCV库的路径,我们添加了本机库
  • 我们下载了tesseract用于图像读取
  • 我们在代码中提供了指向Tesseraact的路径
  • 我们使用System.loadlibrary(Core.NATIVE_LIBRARY_NAME)和openCv.LoadLocal()加载库
  • 然后我们将战争输出用于部署 apache tomcat中没有任何更改或设置

    为了在Tomcat中加载库,我们必须在此处提供一些设置:- 现在对于我们使用的新代码,在代码中加载库静态类(如堆栈溢出上所述的解决方案) 此处System.loadLibrary不工作 我们必须使用System.load和硬编码路径,这会导致内部错误

    我们在静态类中使用了System.load–2次,其中第一个类给出了-std error-bad分配 因为opencv中有两条路径- 这是第一个 加载(“C:\Users\Downloads\opencv\build\bin\opencv_java342.dll”)

    第二个是给出断言错误,根据断言错误将一个保留在上面 这是第二个 加载(“C:\User\Downloads\opencv\build\java\x64\opencv_java3412.dll”)

    代码一直执行到中途,然后退出,到目前为止,代码还没有到达tesseract

    Here is the code for the same :
    
        import java.awt.Image;
        import java.awt.image.BufferedImage;
        import java.awt.image.DataBufferByte;
        import java.io.BufferedWriter;
        import java.io.File;
        import java.io.FileInputStream;
        import java.io.FileWriter;
        import java.io.PrintWriter;
        import java.util.ArrayList;
        import java.util.Iterator;
        import java.util.List;
        import java.util.concurrent.TimeUnit;
        
        import javax.swing.ImageIcon;
        import javax.swing.JFrame;
        import javax.swing.JLabel;
        
        import org.apache.commons.logging.impl.Log4JLogger;
        import org.apache.log4j.Logger;
        import org.apache.poi.ss.usermodel.Cell;
        import org.apache.poi.ss.usermodel.Row;
        import org.apache.poi.xssf.usermodel.XSSFSheet;
        import org.apache.poi.xssf.usermodel.XSSFWorkbook;
        import org.opencv.core.Core;
        import org.opencv.core.CvException;
        import org.opencv.core.Mat;
        import org.opencv.core.MatOfPoint;
        import org.opencv.core.Rect;
        import org.opencv.core.Size;
        import org.opencv.highgui.HighGui;
        import org.opencv.imgcodecs.Imgcodecs;
        import org.opencv.imgproc.Imgproc;
        
        import net.sourceforge.tess4j.Tesseract;
        import nu.pattern.OpenCV;
        
        public class ReadImageBox {
                
            public String readDataFromImage(String imageToReadPath,String tesseractPath) 
            {
        
                String result = "";
                try {
                    String i = Core.NATIVE_LIBRARY_NAME;
                    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
                    
                    logger.info("Img to read "+imageToReadPath);
                    String imagePath =imageToReadPath; // bufferNameOfImagePath = "";
                    logger.info(imagePath);
                
        
                            /*
                             * The class Mat represents an n-dimensional dense numerical single-channel or
                             * multi-channel array. It can be used to store real or complex-valued vectors
                             * and matrices, grayscale or color images, voxel volumes, vector fields, point
                             * clouds, tensors, histograms (though, very high-dimensional histograms may be
                             * better stored in a SparseMat ).
                             */
                            logger.info("imagepath::"+imagePath);
        
                             OpenCV.loadLocally();
                            
                            logger.info("imagepath::"+imagePath);
                            //logger.info("Library Information"+Core.getBuildInformation());
        
                            logger.info("imagepath::"+imagePath);
                            Mat source = Imgcodecs.imread(imagePath);
        
                            logger.info("Source image "+source);
                            String directoryPath = imagePath.substring(0,imagePath.lastIndexOf('/'));
                    
                            
                            logger.info("Going for Image Processing :" + directoryPath);
                            
                            // calling image processing here to process the data from it
                            result = updateImage(100,20,10,3,3,2,source, directoryPath,tesseractPath);  
                            
                            logger.info("Data read "+result);
            return result;
                }
                catch (UnsatisfiedLinkError error) {
                    // Output expected UnsatisfiedLinkErrors.
                    logger.error(error);
                } 
                catch (Exception exception)
                {
                    logger.error(exception);
                }
                return result;
            }
        
            public static String updateImage(int boxSize, int horizontalRemoval, int verticalRemoval, int gaussianBlur,
                    int denoisingClosing, int denoisingOpening, Mat source, String tempDirectoryPath,String tesseractPath) throws Exception{
        
                // Tesseract Object
                logger.info("Tesseract Path :"+tesseractPath);
                Tesseract tesseract = new Tesseract();
                tesseract.setDatapath(tesseractPath);
        
                // Creating the empty destination matrix for further processing
                Mat grayScaleImage = new Mat();``
                Mat gaussianBlurImage = new Mat();
                Mat thresholdImage = new Mat();
                Mat morph = new Mat();
                Mat morphAfterOpreation = new Mat();
                Mat dilate = new Mat();
                Mat hierarchy = new Mat();
        
                logger.info("Image type"+source.type());
                
                // Converting the image to gray scale and saving it in the grayScaleImage matrix
                Imgproc.cvtColor(source, grayScaleImage, Imgproc.COLOR_RGB2GRAY);
                //Imgproc.cvtColor(source, grayScaleImage, 0);
                
                
                // Applying Gaussain Blur
                logger.info("source image "+source);
                
                Imgproc.GaussianBlur(grayScaleImage, gaussianBlurImage, new org.opencv.core.Size(gaussianBlur, gaussianBlur),
                        0);
        
                // OTSU threshold
                Imgproc.threshold(gaussianBlurImage, thresholdImage, 0, 255, Imgproc.THRESH_OTSU | Imgproc.THRESH_BINARY_INV);
        
                logger.info("Threshold image "+gaussianBlur);
                
                // remove the lines of any table inside the invoice
                Mat horizontal = thresholdImage.clone();
                Mat vertical = thresholdImage.clone();
        
                int horizontal_size = horizontal.cols() / 30;
                if(horizontal_size%2==0)
                    horizontal_size+=1;
        
                // showWaitDestroy("Horizontal Lines Detected", horizontal);
        
                Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,
                        new org.opencv.core.Size(horizontal_size, 1));
                Imgproc.erode(horizontal, horizontal, horizontalStructure);
                Imgproc.dilate(horizontal, horizontal, horizontalStructure);
        
                int vertical_size = vertical.rows() / 30;
                if(vertical_size%2==0)
                    vertical_size+=1;
        
                // Create structure element for extracting vertical lines through morphology
                // operations
                Mat verticalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,
                        new org.opencv.core.Size(1, vertical_size));
        
                // Apply morphology operations
                Imgproc.erode(vertical, vertical, verticalStructure);
                Imgproc.dilate(vertical, vertical, verticalStructure);
        
                Core.absdiff(thresholdImage, horizontal, thresholdImage);
                Core.absdiff(thresholdImage, vertical, thresholdImage);
        
                logger.info("Vertical Structure "+verticalStructure);
                
        
                
                Mat newImageFortest = thresholdImage;
                
                
                logger.info("Threshold image "+thresholdImage);
                // applying Closing operation
                Imgproc.morphologyEx(thresholdImage, morph, Imgproc.MORPH_CLOSE, Imgproc.getStructuringElement(
                        Imgproc.MORPH_RECT, new Size(denoisingClosing, denoisingClosing)));
        
                
                logger.info("Morph image "+morph);
                
                // applying Opening operation
                Imgproc.morphologyEx(morph, morphAfterOpreation, Imgproc.MORPH_OPEN, Imgproc.getStructuringElement(
                        Imgproc.MORPH_RECT, new Size(denoisingOpening, denoisingOpening)));
        
                
                logger.info("Morph After operation image "+morphAfterOpreation);
                // Applying dilation on the threshold image to create bounding box edges
                Imgproc.dilate(morphAfterOpreation, dilate,
                        Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(boxSize, boxSize)));
        
                
                logger.info("Dilate image "+dilate);
                
                // creating string buffer object
                String text = "";
                
                try
                {
                
                // finding contours
                List<MatOfPoint> contourList = new ArrayList<MatOfPoint>(); // A list to store all the contours
        
                // finding contours
                Imgproc.findContours(dilate, contourList, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
        
                logger.info("Contour List "+contourList);
                
                
                // Creating a copy of the image
                //Mat copyOfImage = source;
                Mat copyOfImage = newImageFortest;
                
                logger.info("Copy of Image "+copyOfImage);
                
                // Rectangle for cropping
                Rect rectCrop = new Rect();
        
                logger.info("Rectangle Crop New Object "+rectCrop);
        
                // loop through the identified contours and crop them from the image to feed
                // into Tesseract-OCR
        
                for (int i = 0; i < contourList.size(); i++) {
                    // getting bound rectangle
                    rectCrop = Imgproc.boundingRect(contourList.get(i));
        
                    logger.info("Rectangle cropped"+rectCrop);
                    
                    // cropping Image
                    Mat croppedImage = copyOfImage.submat(rectCrop.y, rectCrop.y + rectCrop.height, rectCrop.x,
                            rectCrop.x + rectCrop.width);
        
                    // writing cropped image to disk
                    
                    logger.info("Path to write cropped image "+ tempDirectoryPath);
                    
                    String writePath = tempDirectoryPath + "/croppedImg.png";
                    logger.info("writepath"+writePath);
                    // imagepath = imagepath.
                    Imgcodecs.imwrite(writePath, croppedImage);
        
                    try {
                        // extracting text from cropped image, goes to the image, extracts text and adds
                        // them to stringBuffer
                        
                        logger.info("Exact Path where Image was written with Name "+ writePath);
                        String textExtracted = (tesseract
                                .doOCR(new File(writePath)));
                        
                        //Adding Seperator
                        textExtracted = textExtracted + "_SEPERATOR_";
                        
                        logger.info("Text Extracted "+textExtracted);
                        
                        textExtracted = textExtracted + "\n";
                        text = textExtracted + text;
                        
                        logger.info("Text Extracted Completely"+text);
                        // System.out.println("Andar Ka Text => " + text.toString());
                    } catch (Exception exception) {
                        logger.error(exception);
                    }
                    
                    writePath = "";
                    logger.info("Making write Path empty for next Image "+ writePath);
                }
        
                }
                catch(CvException ae)
                {
                    logger.error("cv",ae);
                }
                catch(UnsatisfiedLinkError ae)
                {
                    logger.error("unsatdif",ae);
                }
                catch(Exception ae)
                {
                    logger.error("general",ae);
                }
                // converting into string
                return text.toUpperCase();      
                
            }
        
            // convert Mat to Image for GUI output
            public static Image toBufferedImage(Mat m) {
                // getting BYTE_GRAY formed image
                int type = BufferedImage.TYPE_BYTE_GRAY;
                if (m.channels() > 1) {
                    type = BufferedImage.TYPE_3BYTE_BGR;
                }
        
                int bufferSize = m.channels() * m.cols() * m.rows();
                byte[] b = new byte[bufferSize];
        
                m.get(0, 0, b); // get all the pixels
        
                // creating buffered Image
                BufferedImage image = new BufferedImage(m.cols(), m.rows(), type);
                final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
                System.arraycopy(b, 0, targetPixels, 0, b.length);
        
                // returning Image
                return image;
            }
        
            // method to display Mat format images using the GUI
            private static void showWaitDestroy(String winname, Mat img) {
                HighGui.imshow(winname, img);
                HighGui.moveWindow(winname, 500, 0);
                HighGui.waitKey(0);
                HighGui.destroyWindow(winname);
            }
        }
    
    以下是相同的代码:
    导入java.awt.Image;
    导入java.awt.image.buffereImage;
    导入java.awt.image.DataBufferByte;
    导入java.io.BufferedWriter;
    导入java.io.File;
    导入java.io.FileInputStream;
    导入java.io.FileWriter;
    导入java.io.PrintWriter;
    导入java.util.ArrayList;
    导入java.util.Iterator;
    导入java.util.List;
    导入java.util.concurrent.TimeUnit;
    导入javax.swing.ImageIcon;
    导入javax.swing.JFrame;
    导入javax.swing.JLabel;
    导入org.apache.commons.logging.impl.Log4JLogger;
    导入org.apache.log4j.Logger;
    导入org.apache.poi.ss.usermodel.Cell;
    导入org.apache.poi.ss.usermodel.Row;
    导入org.apache.poi.xssf.usermodel.xssfheet;
    导入org.apache.poi.xssf.usermodel.xssf工作簿;
    导入org.opencv.core.core;
    导入org.opencv.core.CvException;
    导入org.opencv.core.Mat;
    导入org.opencv.core.MatOfPoint;
    导入org.opencv.core.Rect;
    导入org.opencv.core.Size;
    导入org.opencv.highgui.highgui;
    导入org.opencv.imgcodecs.imgcodecs;
    导入org.opencv.imgproc.imgproc;
    导入net.sourceforge.tess4j.Tesseract;
    导入nu.pattern.OpenCV;
    公共类ReadImageBox{
    公共字符串readDataFromImage(字符串imageToReadPath,字符串tesseractPath)
    {
    字符串结果=”;
    试一试{
    字符串i=Core.NATIVE\u LIBRARY\u NAME;
    System.loadLibrary(Core.NATIVE\u LIBRARY\u NAME);
    logger.info(“要读取的Img”+imagestoreadpath);
    字符串imagePath=imageToReadPath;//bufferNameOfImagePath=“”;
    logger.info(imagePath);
    /*
    *Mat类表示n维密集数值单通道或
    *多通道阵列。可用于存储实数或复数向量
    *矩阵、灰度或彩色图像、体素体积、向量场、点
    *云、张量、直方图(不过,可能需要非常高维的直方图)
    *最好储存在一个小仓库里)。
    */
    logger.info(“imagepath::”+imagepath);
    OpenCV.loadlocal();
    logger.info(“imagepath::”+imagepath);
    //logger.info(“库信息”+Core.getBuildInformation());
    logger.info(“imagepath::”+imagepath);
    Mat source=Imgcodecs.imread(imagePath);
    logger.info(“源图像”+源);
    String directoryPath=imagePath.substring(0,imagePath.lastIndexOf('/');
    info(“正在进行图像处理:“+directoryPath”);
    //在这里调用图像处理来处理来自它的数据
    结果=更新图像(100,20,10,3,3,2,源,目录路径,tesseractPath);
    logger.info(“数据读取”+结果);
    返回结果;
    }
    捕获(未满足链接错误){
    //输出预期未满足的链接错误。
    记录器错误(error);
    } 
    捕获(异常)
    {
    记录器错误(异常);
    }
    返回结果;
    }
    公共静态字符串更新图像(int-boxSize,int-hori