title: How convert a raw image stored in a byte array to a rgb image with opencv and Java
body: <p>i am working in the preview of a fingerprint scaner using id3Fingerprint sdk and OpenCV. If i just show the preview from the id3fingerprint sdk all is fine, but if i load it to a Mat object of OpenCV in order to draw some rectangles in the image then: 
  1.- The fingerprints are displayed in right form but the rectangles are displayed as lines or pixels in random x,y location.
  2.- The rectangles are displayed in right form but the fingerprints are displayed "blured" (look the image attached).<a href="https://i.stack.imgur.com/RUXNy.png" rel="nofollow noreferrer">fingerprints are blured</a></p>
  
  <p>I think, my problem is when i convert the raw grayscale image (a byte array from the id3fingerprint sdk) to a RGB or RGBA image.</p>
  
  <pre><code>private void showPreview2(FingerImage image){
      int height = 750;
      int width = 750;
      int currentWidth = 0;
      int currentHeight = 0;
      try {
          currentWidth = image.getWidth();
          currentHeight = image.getHeight();
      } catch (FingerException ex) {
          Logger.getLogger(CallingID3Example.class.getName()).log(Level.SEVERE, null, ex);
      }
      byte[] pixels = image.getPixels();
      Mat dest = new Mat(); 
      Mat source = new Mat(); 
      Mat source2 = null;
          source2 = new Mat(currentWidth, currentHeight, CvType.CV_8UC1);
          source2.put(0, 0, pixels);
          MatOfByte pix = new MatOfByte();
          Imgcodecs.imencode(".bmp", source2, pix);
          source2.put(0, 0, pix.toArray());
  
          Imgproc.cvtColor(source2, source, Imgproc.COLOR_GRAY2RGBA);
          try {
              int i=0;
              for(FingerImage finger : image.getSegments()){
                  Scalar color;
                          color = new Scalar(0, 250,0);
                  FingerBounds bound = image.getSegmentBounds()[i];
                  Imgproc.rectangle(source, new Point(bound.topLeft.x, bound.topLeft.y),      new Point(bound.bottomRight.x, bound.bottomRight.y), color, 3);
                  double[] pixelTest;
                  pixelTest = source.get(bound.topLeft.x, bound.topLeft.y);
                  i++;
              }
          } catch (FingerException ex) {
              Logger.getLogger(CallingID3Example.class.getName()).log(Level.SEVERE, null, ex);
          }
          gc = canvas.getGraphicsContext2D();
  
          WritableImage writableImage = loadImage(source);
  
          imageView.setImage(writableImage);
  
  }
  
  private WritableImage loadImage(Mat matrix) {
    // Encoding the image
    MatOfByte matOfByte = new MatOfByte();
      Imgcodecs.imencode(".bmp", matrix, matOfByte);
    // Storing the encoded Mat in a byte array
    byte[] byteArray = matOfByte.toArray();
  
    // Displaying the image
    InputStream in = new ByteArrayInputStream(byteArray);
    BufferedImage bufImage = null;
      try {
          bufImage = ImageIO.read(in);
      } catch (IOException ex) {
      }
    // Creating the Writable Image
    WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
    return writableImage;
  }
  </code></pre>
  
  <p>Thanks for your answer.</p>