Image DPI is not changed when converting PNG to JPG

Hi all,

I got an issue when tried to convert file from png to jpg. before convert to jpg I changed resolution, then I used “bos” to upload to server, but at that time the resolution is 96dpi

  1.      File file = new File("C:\\Temp\\input_test.png");
    
  2.      InputStream targetStream = new FileInputStream(file);
    
  3.      ByteArrayOutputStream bos = new ByteArrayOutputStream();
    
  4.      RasterImage rasterImage = (RasterImage)Image.load(targetStream); 
    
  5.      rasterImage.setVerticalResolution(15.0);
    
  6.      rasterImage.setHorizontalResolution(15.0);
    
  7.      rasterImage.save(bos, new JpegOptions());
    
  8.      boolean uploadResults = uploadFiles(bos.toByteArray());
    

I also tries to write “bos” to file instead of uploading to server, I got the same issue, output_test_1.jpg without 15dpi. below code:

  1.             InputStream myInputStream = new ByteArrayInputStream(bos.toByteArray());	        
    
  2.       RasterImage rasterImageTmp = (RasterImage)Image.load(myInputStream);
    
  3.             rasterImageTmp.save("C:\\Temp\\output_test_1.jpg", new JpegOptions());
    

I checked, I replaced line 7 by
rasterImage.save(“C:\Temp\output_test.jpg”, new JpegOptions());
the output_test.jpg file is correct, the resolution is 15dpi

Anyone tell me know why? thank you for any support

@JayTran,

Can you please share source file so that we may further investigate to help you out.

Hi Admad,

I’m using aspose-imaging-19.7-jdk16.jar
below is my source code:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import com.aspose.imaging.Image;
import com.aspose.imaging.RasterImage;

public class TestContentType {

public static void main(String[] args) {
    test();
}

private static void test() {
    try {
        File file = new File("C:\\Temp\\change-space.png");
        InputStream targetStream = new FileInputStream(file);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        RasterImage rasterImage = (RasterImage)Image.load(targetStream); 
        rasterImage.setVerticalResolution(15.0);
        rasterImage.setHorizontalResolution(15.0);
        rasterImage.save(bos, new com.aspose.imaging.imageoptions.JpegOptions());

        System.out.println(rasterImage.getHorizontalResolution()+"  "+ rasterImage.getVerticalResolution());
        
        InputStream myInputStream = new ByteArrayInputStream(bos.toByteArray());	        
        RasterImage rasterImageTmp = (RasterImage)Image.load(myInputStream);
        
        System.out.println(rasterImageTmp.getHorizontalResolution()+"  "+ rasterImageTmp.getVerticalResolution());
        rasterImageTmp.save("C:\\Temp\\change-space1.jpg");
    }
    catch (Exception e) {
        e.printStackTrace();
    }

}

change-space1.jpg with dpi is 96, not 15

are there any ideas for my case?

@JayTran,

Can you please try to use Aspose.Imaging latest version on your end. This will resolve your issue and if there is still an issue than please share generated result along with comparison screenshot so that we may further investigate to help you out.

aspose-image.zip (1.7 MB)
HI Adnan Admad,

I tested with aspose-imaging _19.11, I got the same issue. then I changed a little as below:
ResolutionSetting res = new ResolutionSetting();
res.setHorizontalResolution(20);
res.setVerticalResolution(20);
JpegOptions jpe = new JpegOptions();
jpe.setResolutionSettings(res);
The output result is correct, DPI is 20. but when I check from file by View Detail on Windows, DPI is 96, not 20.
I included input, output file, source file and test result in the attached file.

@JayTran,

I have observed the issue shared by you and have created an issue with ID IMAGINGJAVA-1504 in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

1 Like

Hi mudassir.fayyaz,

please investigate and tell me know it’s status. I’m got stuck to do next steps

Thanks

@JayTran,

I like to inform that we are working on this and will share good news with you soon. I request for your patience.

@Adnan.Ahmad

please take a look this issue(Losing DPI During Image Type Conversion - #2 by babar.raza), do they have relationship?

@JayTran,

At the moment the issue is pending in our issues queue and we may not be able to comment if the two issues are relevant or not although seemingly they same similar from title. We request for your patience till the time the feedback is shared.

Hi mudassir.fayyaz,

Please tell me know status of this issue.

Thanks,

@JayTran,

I have verified from our issue tracking system and regret to share that at present the issue is unresolved. We request for your patience till the time the issue gets resolved.

above issue is resolved by added below code

jpe.setResolutionUnit((byte)ResolutionUnit.Inch);
and using latest version

@JayTran,

Thank you for sharing the valuable feedback with us.

The issues you have found earlier (filed as IMAGINGJAVA-1504) have been fixed in this update.