clear all
clc
% import javax.imageio.*;
% import java.io.ByteArrayOutputStream;
% import java.io.ByteArrayInputStream;
 i1 = imread('ssasa.bmp');
% 
 javaImage = im2java(i1);
 
 H=javaImage.getHeight;
W=javaImage.getWidth;

pixelsData = uint8(javaImage.getBufferedImage.getData.getPixels(0,0,W,H,[]));


% writer = ImageIO.getImageWritersByFormatName("jpg").next();
% 
% baos = ByteArrayOutputStream();
% ss = javax.imageio.ImageIO.write(jImage, "JPG");
% data = baos.toByteArray();
% 
% ImageIO.read([2 1 12 13 1 12 1])

data = pixelsData;
import javax.imageio.ImageIO;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File
dataStream    = ByteArrayInputStream( data );
bufferedimage = ImageIO.read( dataStream );
baos = ByteArrayOutputStream();

ImageIO.write(javaImage.getBufferedImage, "jpg",baos)
% A = uint8(baos.toByteArray);
% 
% fileID = fopen('du.jpg','w'); % Save as a JPEG file because it was received this way
% fwrite(fileID, A);
% I = imread('du.jpg'); % Test if it saved correctly
% imshow(I); 
filename ='C:\Users\tomas\Desktop\dus.jpg' ;
jFile = java.io.File(filename);
ImageIO.write(javaImage.getBufferedImage, "jpg",jFile);
jFile.delete;