Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6820819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:28:13+00:00 2026-05-26T21:28:13+00:00

I’m trying to do a mock-up in Matlab on some image data in a

  • 0

I’m trying to do a mock-up in Matlab on some image data in a database. The image data is from java, output as a base64 encoded byte array. I’m not familiar with the [java image] format. However, I wrote the following Matlab code based on some java written by someone working with me. They follow the same basic outline, and the Java code is able to read the image just fine. The Matlab code looks like this:

function [ result ] = queryDb( theQuery )
  conn   = database( ... ); % connect to the database
  result = fetch( exec( conn, theQuery ) );
  result = result.Data;

  close( conn );
end

data = queryDb( 'sql query to get the data' );
data = uint8( data{1,1} );
data = org.apache.commons.codec.binary.Base64.decodeBase64( data );
data = uint8( 127 + data ); % the base64 decoder returns signed int8

import javax.imageio.ImageIO;
import java.io.ByteArrayInputStream;

dataStream    = ByteArrayInputStream( data );
bufferedimage = ImageIO.read( dataStream );

Upon inspection of bufferedimage, it is an empty array of double as opposed to a java BufferedImage instance.

I ran a few tests on dataStream to see if it behaved as expected; sort of a mini unit test along the lines of:

for jj = 1:10
  for kk 1:10
    assert( dataStream.read() == data(kk) );
  end;

  dataStream.reset();
end;

It checked out, so this leads me to believe the problem is with ImageIO or my use of it.

Unfortunately, none of the examples I’ve found for using ImageIO (and some of these other APIs) are used in quite the manner I outline here (in Matlab that is).

This code uses java.io.ByteArrayInputStream in the same manner — in the sense that the provided data is an array of bytes.

This code is essentially what I’m looking to do — convert a java image to a Matlab array. Unfortunately, they cheat by taking a Matlab image, turning it into a java image, then turn it back.

This code uses ImageIO, but it does so by reading from a file stream. I tried writing the data out to a file then reading it in using java.io.File, but I get the same result either way.

So, I’m at a loss.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:28:14+00:00Added an answer on May 26, 2026 at 9:28 pm

    The approach outlined in the question works … the only thing I have that differs in any way from the code above is I’m not adding 127 to the uint8 array:

    % Do all the imports
    % sql query to get the data
    dataStream    = ByteArrayInputStream( decodeBase64( rows.data{1} ) );
    bufferedImage = ImageIO.read( dataStream );
    [w h] = deal( theWidth, theHeight );
    imageData     = uint8( bufferedImage.getData.getPixels( 0,0,w,h,[] ) );
    imageData     = permute( reshape( imageData, [3 w h] ), [2 3 1] );
    
    imshow( imageData );
    % voila
    

    edit Incorporating the text @kchja linked to, in case that link becomes invalid:

    Subject: How can I convert a “Java Image” object into a MATLAB image
    matrix?

    Problem Description: The IM2JAVA function is available for converting
    a MATLAB image into a Java image. I would like to convert a Java image
    to a MATLAB image.

    Solution: There is no built-in function to convert Java images into
    MATLAB images. However using the Java API you can extract the data
    from a Java image and store it as a matrix, which is MATLAB’s image
    representation.

    Below is an example of converting a MATLAB image to Java and back to a
    MATLAB image. The image used in this example is part of the MATLAB
    demos and is on the MATLAB path. In this example, the “getPixels”
    function of the Java class “Raster” returns the RGB values for the
    image.

    Depending upon the format of your Java image, additional work may be
    required. Java images can be stored in one of many formats; the
    “getPixels” function returns the pixel data in that format. For more
    information, see the javadoc page for the java.awt.image.Raster class
    at http://java.sun.com/j2se/1.5.0/docs/api/java/awt/image/Raster.html

    % create java image
    I = imread('office_3.jpg');
    javaImage = im2java(I);
    
    % get image properties
    H=javaImage.getHeight;
    W=javaImage.getWidth;
    
    % repackage as a 3D array (MATLAB image format)
    B = uint8(zeros([H,W,3]));
    pixelsData = uint8(javaImage.getBufferedImage.getData.getPixels(0,0,W,H,[]));
    for i = 1 : H
    base = (i-1)*W*3+1;
    B(i,1:W,:) = deal(reshape(pixelsData(base:(base+3*W-1)),3,W)');
    end
    
    % display image
    imshow(B);
    

    The following are two other ways to implement this (in a more
    optimized manner):

    Example 1: (Faster execution)

    pixelsData = reshape(typecast(jImage.getData.getDataStorage, 'uint8'), 4, w, h);
    imgData = cat(3, ...
            transpose(reshape(pixelsData(3, :, :), w, h)), ...
            transpose(reshape(pixelsData(2, :, :), w, h)), ...
            transpose(reshape(pixelsData(1, :, :), w, h)));
    

    Example 2:

    imgData = zeros([H,W,3],'uint8');
    pixelsData = reshape(typecast(javaImage.getBufferedImage.getData.getDataStorage,'uint32'),W,H).';
    imgData(:,:,3) = bitshift(bitand(pixelsData,256^1-1),-8*0);
    imgData(:,:,2) = bitshift(bitand(pixelsData,256^2-1),-8*1);
    imgData(:,:,1) = bitshift(bitand(pixelsData,256^3-1),-8*2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.