Ask Your Question

(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( at facetracker.DisplayingImagesUsingSwings.main(

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

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

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

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

1 answer

Sort by ยป oldest newest most voted

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


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


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

Seen: 1,173 times

Last updated: Jan 27