Free Support Forum - aspose.com

Problems with jpg image conversion


#1

jpegWithInvalidExifSegments.jpg (118.3 KB)

Hello,

We have a particular image that we are converting to jpeg. Some code in our system converts jpg to jpg for legacy reasons. When converting to jpg, it creates an image that cannot be opened by Windows Photo Viewer. It successfully converts to PNG. Can you please advise? Can validate issue with aspose-imaging 19.1.

Here is some sample code:

private static byte[] convertImageToJPEG(RasterImage image, int defaultXDPI, int defaultYDPI)
{
    long imageType = image.getFileFormat();

    // The Aspose API currently does not support gif manipulation. In order to ensure that the transparency of a gif image is ccnverted to a white background, convert to png which preserves

// transparency and then can be removed.
if (imageType == GIF_IMAGE_TYPE)
{
byte[] pngByteArray = convertImageToPNG(image, defaultXDPI, defaultYDPI);
image = (RasterImage) Image.load(new ByteArrayInputStream(pngByteArray));
}

    ByteArrayOutputStream result = new ByteArrayOutputStream();
    try
    {
        // gist.github.com/aspose-imaging/8c9bd83e0d07145ba0c1#file-examples-src-main-java-com-aspose-imaging-examples-manipulatingpngimages-changebackgroundcolor-changebackgroundcolor-java
        int[] pixels = image.loadArgb32Pixels(image.getBounds());

        for (int i = 0; i < pixels.length; i++)
        {
            if (pixels[i] == image.getTransparentColor().toArgb())
            {
                pixels[i] = com.aspose.imaging.Color.getWhite().toArgb();
            }
        }
        image.saveArgb32Pixels(image.getBounds(), pixels);

        ImageOptionsBase originalOptions = image.getOriginalOptions();

        JpegOptions jpegOptions = new JpegOptions();

        if (originalOptions != null)
        {
            jpegOptions.setResolutionSettings(originalOptions.getResolutionSettings());
        }
        else
        {
            jpegOptions.setResolutionSettings(new ResolutionSetting(defaultXDPI, defaultYDPI));
        }

        image.save(result, jpegOptions);
    }
    finally
    {
        image.close();
    }

    return result.toByteArray();
}

#2

@drakegens,

I have observed the issue shared by you and request you to please share the generated output that fails to open in Windows Photo viewer. Please also provide the values for function parameter defaultXDPI and defaultYDPI for reference as well so that we may be able to use the code.


#3

jpegWithInvalidExifSegments.jpg (118.3 KB)
output1.jpg (179 Bytes)

I have provided both input and output image. defaultXDPI = 72 and defaultYDPI = 72.


#4

@drakegens,

I have observed issue shared by you. An issue with ID IMAGINGJAVA-1181 has been created in our issue tracking system to investigate and resolve the issue. This thread has been linked with these issues so that you may be automatically notified once the issues will be resolved.


#5

I’ve done some more investigation, and i’ve noticed the issue does not occur with a 64 bit jdk. But it does occur with a 32 bit jdk.


#6

@drakegens,

Thank you for your feedback. Secondly, your issue seems similar to one shared by you in another thread as well. Can we disregard the following thread then.


#7

Yes the issues are similar, we can disregard the other thread. I did try the sample code. I am noticing different behaviors between the 32 bit jdk and the 64 bit jdk.


#8

@drakegens,

Your feedback has been shared. We will get back to you with good news soon.


#9

@Adnan.Ahmad Is there any update on this?

Thanks,

Drake Gens


#10

@drakegens,

I have verified the issue status from our issue tracking system and like to share that issue has just recently been created in our issue tracking system and is pending for investigation in issues queue at the moment. We request for your patience till the time the issue gets resolved.


#11

@drakegens,

I suggest you to please try using following sample code on your end using Aspose.Imaging for Java 19.1. I hope the shared information will be helpful.

RasterImage image = (RasterImage)Image.load("jpegWithInvalidExifSegments.jpg");
try
{
    int[] pixels = image.loadArgb32Pixels(image.getBounds());

    int transparent = image.getTransparentColor().toArgb();
    int whiteArgb = Color.getWhite().toArgb();
    for (int i = 0; i < pixels.length; i++)
    {
        if (pixels[i] == transparent)
        {
            pixels[i] = whiteArgb;
        }
    }
    image.saveArgb32Pixels(image.getBounds(), pixels);

    ImageOptionsBase originalOptions = image.getOriginalOptions();

    JpegOptions jpegOptions = new JpegOptions();

    if (originalOptions != null)
    {
        jpegOptions.setResolutionSettings(originalOptions.getResolutionSettings());
    }
    else
    {
        jpegOptions.setResolutionSettings(new ResolutionSetting(72, 72));
    }

    image.save("out_java.jpg", jpegOptions);
}
finally
{
    image.close();
}