-
Notifications
You must be signed in to change notification settings - Fork 32
[INJICERT-1226] Add image-compressor utility for mosipid plugin #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
01a5697
[INJICERT-1226] mosipid-image-compressor
Piyush7034 2743f2f
[INJICERT-1226] Add configs for maxSize and retryAttempts
Piyush7034 3813a5a
[INJICERT-1222] Address coderabbit review
Piyush7034 0b116ec
[INJICERT-1226] Update keys for compressed face as compressedPicture
Piyush7034 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...plugin/src/main/java/io/mosip/certify/mosipid/integration/helper/ImageCompressorUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package io.mosip.certify.mosipid.integration.helper; | ||
|
|
||
| import io.mosip.biometrics.util.CommonUtil; | ||
| import io.mosip.certify.api.exception.DataProviderExchangeException; | ||
| import io.mosip.certify.mosipid.integration.service.ImageCompressorServiceImpl; | ||
| import io.mosip.kernel.biometrics.entities.*; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.Base64; | ||
|
|
||
| @Component | ||
| @Slf4j | ||
| public class ImageCompressorUtil { | ||
| private final ImageCompressorServiceImpl service; | ||
|
|
||
| @Autowired | ||
| public ImageCompressorUtil(ImageCompressorServiceImpl service) { | ||
| this.service = service; | ||
| } | ||
|
|
||
| @Value("${mosip.certify.image-compressor.image.max-allowed-size:4096}") | ||
| private int maxAllowedImageSize; | ||
|
|
||
| @Value("${mosip.certify.image-compressor.image.max-retry-attempts:3}") | ||
| private int maxRetryAttempts; | ||
|
|
||
|
|
||
| public byte[] compressImage(byte[] imageBytes) { | ||
| return service.doResizeAndCompress(imageBytes); | ||
| } | ||
|
|
||
| public String extractAndCompressImage(String imageData) throws DataProviderExchangeException { | ||
| try { | ||
| // --- Require Data URI with prefix only --- | ||
| if (imageData == null || imageData.isBlank() || !imageData.startsWith("data:") || !imageData.contains(";") | ||
| || !imageData.contains(",")) { | ||
| throw new IllegalArgumentException("Invalid image format. Upload a proper image type."); | ||
| } | ||
|
|
||
| // Basic structure guards | ||
| int colon = imageData.indexOf(':'); // should be 4 ("data:") | ||
| int semi = imageData.indexOf(';'); | ||
| int comma = imageData.indexOf(','); | ||
| if (colon < 0 || semi < 0 || comma < 0 || colon >= semi || semi >= comma) { | ||
| throw new IllegalArgumentException("Invalid image format. Upload a proper image type."); | ||
| } | ||
|
|
||
| // Extract MIME (e.g., image/png, image/jpeg) | ||
| String mimeType = imageData.substring(colon + 1, semi).trim(); | ||
|
|
||
| // Extract the format (e.g., "png", "jpeg", "jpg"); default "" if malformed | ||
| int slash = mimeType.indexOf('/'); | ||
| String formatName = (slash >= 0 && slash < mimeType.length() - 1) | ||
| ? mimeType.substring(slash + 1).toLowerCase() | ||
| : ""; | ||
|
|
||
| // Fallback rule: anything other than png/jpeg/jpg → force JPEG | ||
| boolean isPng = "png".equals(formatName); | ||
| boolean usePng = isPng; // only true when explicitly PNG | ||
|
|
||
| // Extract Base64 payload and decode | ||
| String base64Data = imageData.substring(comma + 1).trim(); | ||
| byte[] inputBytes = Base64.getDecoder().decode(base64Data); | ||
|
|
||
| // Compress (assumed JP2 output) | ||
| int attempts = 0; | ||
| byte[] jp2Bytes; | ||
|
|
||
| while (true) { | ||
| jp2Bytes = compressImage(inputBytes); | ||
| attempts++; | ||
|
|
||
| if (jp2Bytes.length <= maxAllowedImageSize) { | ||
| break; | ||
| } | ||
| if (attempts >= maxRetryAttempts) { | ||
| throw new DataProviderExchangeException( | ||
| "FACE_IMAGE_TOO_LARGE", | ||
| "Unable to compress image with available compression. Check size or quality of the input image." | ||
| ); | ||
| } | ||
|
|
||
| // use the last compressed output as the next input | ||
| inputBytes = jp2Bytes; | ||
| } | ||
|
|
||
| // Convert JP2 → desired output format | ||
| final byte[] outBytes; | ||
| final String outMime; | ||
| if (usePng) { | ||
| outBytes = CommonUtil.convertJP2ToPNGBytes(jp2Bytes); | ||
| outMime = "image/png"; | ||
| } else { | ||
| outBytes = CommonUtil.convertJP2ToJPEGBytes(jp2Bytes); | ||
| outMime = "image/jpeg"; | ||
| } | ||
|
|
||
| // Encode and return as Data URI | ||
| final String b64 = Base64.getEncoder().encodeToString(outBytes); | ||
| return "data:" + outMime + ";base64," + b64; | ||
|
|
||
| } catch (IllegalArgumentException iae) { | ||
| log.error("ERROR_PARSING_IMAGE_DATA", iae); | ||
| throw new DataProviderExchangeException("ERROR_PARSING_IMAGE_DATA", iae.getMessage()); | ||
| } catch (DataProviderExchangeException e) { | ||
| log.error("MAX_ATTEMPTS_REACHED", e); | ||
| throw e; | ||
| } catch (Exception e) { | ||
| log.error("Image compression failed", e); | ||
| throw new DataProviderExchangeException( | ||
| "ERROR_COMPRESSING_IMAGE", | ||
| "Failed to compress image data. Check the image format and other properties." | ||
| ); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...rc/main/java/io/mosip/certify/mosipid/integration/service/ImageCompressorServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package io.mosip.certify.mosipid.integration.service; | ||
|
|
||
| import io.mosip.image.compressor.sdk.service.ImageCompressionService; | ||
| import io.mosip.kernel.biometrics.constant.BiometricType; | ||
| import io.mosip.kernel.biometrics.entities.BiometricRecord; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.core.env.Environment; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
|
|
||
| @Component | ||
| public class ImageCompressorServiceImpl extends ImageCompressionService { | ||
| @Autowired | ||
| public ImageCompressorServiceImpl(Environment env) { | ||
| super(env, new BiometricRecord(), List.of(BiometricType.FACE), new HashMap<>()); | ||
| } | ||
|
|
||
| public byte[] doResizeAndCompress(byte[] imageBytes) { | ||
| return resizeAndCompress(imageBytes); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.