Ask Your Question
0

(JAVA) - Raw image encoder error: Empty JPEG image (DNL not supported) in function 'cv::BaseImageEncoder::throwOnEror'

asked 2019-01-27 00:04:21 -0500

jackson gravatar image

hi, The following error is occurring:

Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: OpenCV(4.0.1) C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\imgcodecs\src\grfmt_base.cpp:145: error: (-10:Unknown error code -10) Raw image encoder error: Empty JPEG image (DNL not supported) in function 'cv::BaseImageEncoder::throwOnEror' ] at org.opencv.imgcodecs.Imgcodecs.imencode_1(Native Method) at org.opencv.imgcodecs.Imgcodecs.imencode(Imgcodecs.java:164) at facetracker.DisplayingImagesUsingSwings.main(DisplayingImagesUsingSwings.java:31)

follows code for testing, I'm using opencv 4.0.1 java and windows 10. the opencv_java401.dll DLL is configured correctly in eclipse in native library location.

import java.awt.image.BufferedImage;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;

public class DisplayingImagesUsingSwings {
   public static void main(String args[]) throws Exception { 
      //Loading the OpenCV core library  
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); 

      //Reading the Image from the file and storing it in to a Matrix object 
      String file = "C:/EXAMPLES/OpenCV/sample.jpg"; 
      Mat image = Imgcodecs.imread(file); 

      //Encoding the image 
      MatOfByte matOfByte = new MatOfByte();       
      Imgcodecs.imencode(".jpg", image, matOfByte); 

      //Storing the encoded Mat in a byte array 
      byte[] byteArray = matOfByte.toArray(); 

      //Preparing the Buffered Image 
      InputStream in = new ByteArrayInputStream(byteArray); 
      BufferedImage bufImage = ImageIO.read(in); 

      //Instantiate JFrame 
      JFrame frame = new JFrame(); 

      //Set Content to the JFrame 
      frame.getContentPane().add(new JLabel(new ImageIcon(bufImage))); 
      frame.pack(); 
      frame.setVisible(true);

      System.out.println("Image Loaded");     
   } 
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-01-27 01:37:58 -0500

berak gravatar image

updated 2019-01-27 02:39:26 -0500

the image you're trying to encode was never loaded / is empty() !

please add a check after the imread() call:

String file = "C:/EXAMPLES/OpenCV/sample.jpg"; 
Mat image = Imgcodecs.imread(file); 
if (image.empty()) {
      // can't go on !
}
edit flag offensive delete link more

Comments

thank my friend, with this check I saw that I was not reading the file. Thank you very much!

jackson gravatar imagejackson ( 2019-01-27 11:17:10 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-01-27 00:04:21 -0500

Seen: 1,340 times

Last updated: Jan 27