HEIC to JPG conversion, Or...?

1718
5
Jump to solution
06-10-2023 01:35 PM
Labels (2)
DavidPuckett
Occasional Contributor

I have a feature that allows my users to select pictures from their device's picture gallery and add as an attachment to a feature. However, since arcgis does not yet support the HEIC image format and iOS uses this format by default, a user cannot use a photo taken from their device as an attachment.

I imagine that the solution is to convert the HEIC to JPEG and that the arcgis apps like Survey 123 do that under the hood. However, I cannot find a resource that converts from HEIC to JPEG in Qt\QML or within the AppStudio Framework itself. I am using the AppStudio Framework DocumentDialog, per the Edit Feature Attachments example (note that the sample also only supports jpeg).

So, questions:
Has anyone found a way to convert from HEIC to JPEG in an AppStudio app? This is ideal.

Has anyone implemented a workaround for this issue? I know about changing the picture format on an iOS device but that doesn't help for photos already taken and we don't always have the control to tell users to do that. For reference:
Settings > Camera > Formats > Most Compatible ✔️

Thanks for any insights

1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Hi,

QuickReport sample contains that piece of code:

            var outputFileInfo = outputFolder.fileInfo(outputFileName);

            //workaround for HEIC file format to be converted to jpg
            if (outputFileName.match(/\.hei(c|f)$/i)) {
                imageObject.load(picturePath);

                outputFileName = outputFileName.replace(/\.[a-z]*$/i, ".jpg");
                outputFileInfo = outputFolder.fileInfo(outputFileName);

                outputFolder.removeFile(outputFileName);

                if (!imageObject.save(outputFileInfo.filePath)) {
                    console.error("Unable to save image:", outputFileInfo.filePath);
                    return;
                }
                picturePath = outputFolder.filePath(outputFileName);
            } else {
                //imageObject.load(pictureUrl)
                outputFolder.removeFile(outputFileName);
                var issaved = imageObject.save(outputFileInfo.filePath)
                //outputFolder.copyFile(picturePath, outputFileInfo.filePath);
            }

            picturePath = outputFolder.filePath(outputFileName);

View solution in original post

0 Kudos
5 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

QuickReport sample contains that piece of code:

            var outputFileInfo = outputFolder.fileInfo(outputFileName);

            //workaround for HEIC file format to be converted to jpg
            if (outputFileName.match(/\.hei(c|f)$/i)) {
                imageObject.load(picturePath);

                outputFileName = outputFileName.replace(/\.[a-z]*$/i, ".jpg");
                outputFileInfo = outputFolder.fileInfo(outputFileName);

                outputFolder.removeFile(outputFileName);

                if (!imageObject.save(outputFileInfo.filePath)) {
                    console.error("Unable to save image:", outputFileInfo.filePath);
                    return;
                }
                picturePath = outputFolder.filePath(outputFileName);
            } else {
                //imageObject.load(pictureUrl)
                outputFolder.removeFile(outputFileName);
                var issaved = imageObject.save(outputFileInfo.filePath)
                //outputFolder.copyFile(picturePath, outputFileInfo.filePath);
            }

            picturePath = outputFolder.filePath(outputFileName);
0 Kudos
DavidPuckett
Occasional Contributor

Thank you for pointing me in the right direction @GKmieliauskas . This sample shows that the ImageObject component is the hero here. Simple and wildly undocumented. Turns out if you load the heic image into imageObject and replace the .heic extension with .jpg extension in the destination path the imageObject will make the conversion when calling the save method. Would love to see this documented but hopefully anyone else with this requirement can find this post. Thanks again, looks like I have my solution. :collision:

sample that works

// first make the folder if it doesn't exist already
fileFolder.makeFolder();
// now create the full destination path by getting the path to the new folder along with our new pic file name
let destination = fileFolder.filePath(picFileName);

// load our picture into imageObject in prep for save 
imageObject.load(fileInfo.filePath);
// if we have a heic format picture swap the .heic suffix for the .jpg suffix 
// *note that this is an undocumented hero act that isn't found in readily available Qt\QML libraries
if (fileDialog.fileUrl.toString().toLowerCase().includes("heic")) {
    destination = destination.replace(".heic", ".jpg");
}

// now that the selected image is loaded into imageObject
// and we have created our destination path 
// call the save method
let copied = imageObject.save(destination);
0 Kudos
GKmieliauskas
Esri Regular Contributor
0 Kudos
DavidPuckett
Occasional Contributor

Maybe a better description is under-documented. The save method converting from heic to jpg is HUGE due to the usage by Apple iOS but isn't mentioned at all in the docs. It's a significant feature that is easily missed, as I did. Thanks again for the direction.

0 Kudos
DavidPuckett
Occasional Contributor

Oh my, the latest version of Let Me Google That For You! Let Me GPT That For You? 🤣

Ummm, thank you? Now off to manage my podcasts! 🚀

0 Kudos