Main Content

Render MATLAB Image Data in Java

This section contains code snippets intended to demonstrate specific functionality related to working with figure and image data.

Working with Images

Get Encoded Image Bytes from Image in Component

public byte[] getByteArrayFromDeployedComponent()
{
    Object[] byteImageOutput = null;
    MWNumericArray numericImageByteArray = null;
    try
    {
        byteImageOutput =
            deployment.getImageDataOrientation(
                1,      //Number Of Outputs    
                500,    //Height
                500,    //Width
                30,     //Elevation
                30,     //Rotation
                "png"   //Image Format
            );
        
        numericImageByteArray = 
              (MWNumericArray)byteImageOutput[0];
        return numericImageByteArray.getByteData();
    }
    finally
    {
        MWArray.disposeArray(byteImageOutput);
    }
}

Get Buffered Image in Component

public byte[] getByteArrayFromDeployedComponent()
{
    Object[] byteImageOutput = null;
    MWNumericArray numericImageByteArray = null;
    try
    {
        byteImageOutput =
            deployment.getImageDataOrientation(
                1,      //Number Of Outputs    
                500,    //Height
                500,    //Width
                30,     //Elevation
                30,     //Rotation
                "png"   //Image Format
            );
        
        numericImageByteArray = 
                 (MWNumericArray)byteImageOutput[0];
        return numericImageByteArray.getByteData();
    }
    finally
    {
        MWArray.disposeArray(byteImageOutput);
    }
}

public BufferedImage getBufferedImageFromDeployedComponent()
{
    try
    {
        byte[] imageByteArray = 
               getByteArrayFromDeployedComponent()
        return ImageIO.read
                (new ByteArrayInputStream(imageByteArray));
    }
    catch(IOException io_ex)
    {
        io_ex.printStackTrace();
    }
}

Create Buffered Images from MATLAB Array

Use the renderArrayData method to:

  • Create a buffered image from data in a given MATLAB® array.

  • Verify the array is of three dimensions (height, width, and color component).

  • Verify the color component order is red, green, and blue.

    See renderArrayData in the Java® API documentation for information on input parameters, return values, exceptions thrown, and examples.