Java Code Examples for javafx.scene.image.PixelReader#getPixels()

The following examples show how to use javafx.scene.image.PixelReader#getPixels() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FxNinePatch.java    From NinePatch with Apache License 2.0 6 votes vote down vote up
@Override
public int[] getPixels(Image img, int x, int y, int w, int h)
{
    int[] pixels = new int[w * h];

    PixelReader reader = img.getPixelReader();

    PixelFormat.Type type =  reader.getPixelFormat().getType();

    WritablePixelFormat<IntBuffer> format = null;

    if(type == PixelFormat.Type.INT_ARGB_PRE)
    {
        format = PixelFormat.getIntArgbPreInstance();
    }
    else
    {
        format = PixelFormat.getIntArgbInstance();
    }

    reader.getPixels(x, y, w, h, format, pixels, 0, w);

    return pixels;
}
 
Example 2
Source File: OpenCVUtils.java    From OpenLabeler with Apache License 2.0 5 votes vote down vote up
/**
 * Return OpenCV MAT in CvType.CV_8UC4
 */
public static Mat imageToMat(Image image) {
   int width = (int) image.getWidth();
   int height = (int) image.getHeight();
   byte[] buffer = new byte[width * height * 4];

   PixelReader reader = image.getPixelReader();
   WritablePixelFormat<ByteBuffer> format = WritablePixelFormat.getByteBgraInstance();
   reader.getPixels(0, 0, width, height, format, buffer, 0, width * 4);

   Mat mat = new Mat(height, width, CvType.CV_8UC4);
   mat.put(0, 0, buffer);
   return mat;
}