diff --git a/biometrics-util/src/main/java/io/mosip/biometrics/util/CommonUtil.java b/biometrics-util/src/main/java/io/mosip/biometrics/util/CommonUtil.java index 370ad7f5370..68da68f9ecb 100644 --- a/biometrics-util/src/main/java/io/mosip/biometrics/util/CommonUtil.java +++ b/biometrics-util/src/main/java/io/mosip/biometrics/util/CommonUtil.java @@ -1,15 +1,22 @@ package io.mosip.biometrics.util; +import io.mosip.biometrics.util.face.FaceDecoder; import org.jnbis.api.model.Bitmap; import org.jnbis.internal.WsqDecoder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.awt.image.WritableRaster; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; public class CommonUtil { + private static final Logger LOGGER = LoggerFactory.getLogger(CommonUtil.class); + public static BufferedImage getBufferedImage(ConvertRequestDto convertRequestDto) throws Exception { BufferedImage bufferedImage = null; switch (convertRequestDto.getImageType()) { @@ -34,4 +41,14 @@ private static BufferedImage convert(Bitmap bitmap) { raster.setDataElements(0, 0, width, height, data); return image; } + + public static byte[] convertJP2ToJPEGBytes(byte[] jp2000Bytes) { + try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + ImageIO.write(ImageIO.read(new ByteArrayInputStream(jp2000Bytes)), "jpg", baos); + return baos.toByteArray(); + } catch (IOException e) { + LOGGER.error("Failed to get jpg image", e); + } + return null; + } } diff --git a/biometrics-util/src/main/java/io/mosip/biometrics/util/face/FaceDecoder.java b/biometrics-util/src/main/java/io/mosip/biometrics/util/face/FaceDecoder.java index 534fee93f81..38a14e48295 100644 --- a/biometrics-util/src/main/java/io/mosip/biometrics/util/face/FaceDecoder.java +++ b/biometrics-util/src/main/java/io/mosip/biometrics/util/face/FaceDecoder.java @@ -1,15 +1,20 @@ package io.mosip.biometrics.util.face; +import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.IOException; +import io.mosip.biometrics.util.CommonUtil; import io.mosip.biometrics.util.ConvertRequestDto; import io.mosip.biometrics.util.iris.IrisBDIR; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.imageio.ImageIO; + public class FaceDecoder { private static final Logger LOGGER = LoggerFactory.getLogger(FaceDecoder.class); @@ -32,16 +37,45 @@ public static FaceBDIR getFaceBDIR(ConvertRequestDto convertRequestDto) throws E throw new UnsupportedOperationException(); } - private static byte [] convertFaceISO19794_5_2011ToImage(byte [] isoData) throws Exception + private static byte[] convertFaceISO19794_5_2011ToImage(byte [] isoData) throws Exception + { + ImageData imageData = getFaceBDIRISO19794_5_2011 (isoData).getRepresentation() + .getRepresentationData().getImageData(); + try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + ImageIO.write(ImageIO.read(new ByteArrayInputStream(imageData.getImage())), "jpg", baos); + return baos.toByteArray(); + } catch (IOException e) { + LOGGER.error("Failed to get jpg image", e); + } + return imageData.getImage(); + } + + public static BufferedImage convertFaceISOToBufferedImage(ConvertRequestDto convertRequestDto) throws Exception { - return getFaceBDIRISO19794_5_2011 (isoData).getRepresentation().getRepresentationData().getImageData().getImage(); + switch (convertRequestDto.getVersion()) { + case "ISO19794_5_2011" : + FaceBDIR faceBDIR = getFaceBDIRISO19794_5_2011 (convertRequestDto.getInputBytes()); + return ImageIO.read(new ByteArrayInputStream(faceBDIR.getRepresentation() + .getRepresentationData().getImageData().getImage())); + } + throw new UnsupportedOperationException(); } - public static byte [] convertFaceISOToImage(ConvertRequestDto convertRequestDto) throws Exception + public static byte[] convertFaceISOToImageBytes(ConvertRequestDto convertRequestDto) throws Exception { switch (convertRequestDto.getVersion()) { case "ISO19794_5_2011" : - return convertFaceISO19794_5_2011ToImage(convertRequestDto.getInputBytes()); + FaceBDIR faceBDIR = getFaceBDIRISO19794_5_2011 (convertRequestDto.getInputBytes()); + ImageDataType imageDataType = faceBDIR.getRepresentation().getRepresentationHeader().getImageInformation().getImageDataType(); + switch (imageDataType) { + case JPEG2000_LOSSY: + case JPEG2000_LOSS_LESS: + return CommonUtil.convertJP2ToJPEGBytes(faceBDIR.getRepresentation() + .getRepresentationData().getImageData().getImage()); + default: + return faceBDIR.getRepresentation() + .getRepresentationData().getImageData().getImage(); + } } throw new UnsupportedOperationException(); } diff --git a/biometrics-util/src/main/java/io/mosip/biometrics/util/finger/FingerDecoder.java b/biometrics-util/src/main/java/io/mosip/biometrics/util/finger/FingerDecoder.java index f54bbd19857..46cfa13d6cd 100644 --- a/biometrics-util/src/main/java/io/mosip/biometrics/util/finger/FingerDecoder.java +++ b/biometrics-util/src/main/java/io/mosip/biometrics/util/finger/FingerDecoder.java @@ -1,13 +1,18 @@ package io.mosip.biometrics.util.finger; +import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.IOException; +import io.mosip.biometrics.util.CommonUtil; import io.mosip.biometrics.util.ConvertRequestDto; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.imageio.ImageIO; + public class FingerDecoder { private static final Logger LOGGER = LoggerFactory.getLogger(FingerDecoder.class); @@ -30,16 +35,32 @@ public static FingerBDIR getFingerBDIR(ConvertRequestDto convertRequestDto) thro throw new UnsupportedOperationException(); } - private static byte [] convertFingerISO19794_4_2011ToImage(byte [] isoData) throws Exception + + public static byte [] convertFingerISOToImageBytes(ConvertRequestDto convertRequestDto) throws Exception { - return getFingerBDIRISO19794_4_2011 (isoData).getRepresentation().getRepresentationBody().getImageData().getImage(); + switch (convertRequestDto.getVersion()) { + case "ISO19794_4_2011" : + FingerBDIR fingerBDIR = getFingerBDIRISO19794_4_2011 (convertRequestDto.getInputBytes()); + FingerImageCompressionType fingerImageCompressionType = fingerBDIR.getRepresentation().getRepresentationHeader().getCompressionType(); + switch (fingerImageCompressionType) { + case JPEG_2000_LOSSY: + case JPEG_2000_LOSS_LESS: + return CommonUtil.convertJP2ToJPEGBytes(fingerBDIR.getRepresentation().getRepresentationBody().getImageData().getImage()); + default: + return fingerBDIR.getRepresentation().getRepresentationBody().getImageData().getImage(); + } + } + throw new UnsupportedOperationException(); } - public static byte [] convertFingerISOToImage(ConvertRequestDto convertRequestDto) throws Exception + public static BufferedImage convertFingerISOToBufferedImage(ConvertRequestDto convertRequestDto) throws Exception { + ImageData imageData = null; switch (convertRequestDto.getVersion()) { case "ISO19794_4_2011" : - return convertFingerISO19794_4_2011ToImage(convertRequestDto.getInputBytes()); + imageData = getFingerBDIRISO19794_4_2011 (convertRequestDto.getInputBytes()). + getRepresentation().getRepresentationBody().getImageData(); + return ImageIO.read(new ByteArrayInputStream(imageData.getImage())); } throw new UnsupportedOperationException(); } diff --git a/biometrics-util/src/main/java/io/mosip/biometrics/util/iris/ImageFormat.java b/biometrics-util/src/main/java/io/mosip/biometrics/util/iris/ImageFormat.java index ee28cec7bad..db14849974a 100644 --- a/biometrics-util/src/main/java/io/mosip/biometrics/util/iris/ImageFormat.java +++ b/biometrics-util/src/main/java/io/mosip/biometrics/util/iris/ImageFormat.java @@ -8,10 +8,10 @@ public enum ImageFormat { RGB_RAW(0x0004), MONO_JPEG(0x0006), RGB_JPEG(0x0008), - MONO_JPEG_LOSS_LESS(0x000A), - RGB_JPEG_LOSS_LESS(0x000C), - MONO_JPEG2000(0x000E), - RGB_JPEG2000(0x0010); + MONO_JPEG2000_LOSS_LESS(0x000A), + RGB_JPEG2000_LOSS_LESS(0x000C), + MONO_PNG(0x000E), + RGB_PNG(0x0010); private final int value; ImageFormat(int value) { diff --git a/biometrics-util/src/main/java/io/mosip/biometrics/util/iris/IrisDecoder.java b/biometrics-util/src/main/java/io/mosip/biometrics/util/iris/IrisDecoder.java index 5083448cd5f..de4be9c0e12 100644 --- a/biometrics-util/src/main/java/io/mosip/biometrics/util/iris/IrisDecoder.java +++ b/biometrics-util/src/main/java/io/mosip/biometrics/util/iris/IrisDecoder.java @@ -1,15 +1,20 @@ package io.mosip.biometrics.util.iris; +import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.IOException; +import io.mosip.biometrics.util.CommonUtil; import io.mosip.biometrics.util.ConvertRequestDto; import io.mosip.biometrics.util.finger.FingerBDIR; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.imageio.ImageIO; + public class IrisDecoder { private static final Logger LOGGER = LoggerFactory.getLogger(IrisDecoder.class); @@ -34,14 +39,42 @@ public static IrisBDIR getIrisBDIR(ConvertRequestDto convertRequestDto) throws E private static byte [] convertIrisISO19794_6_2011ToImage(byte [] isoData) throws Exception { - return getIrisBDIRISO19794_6_2011 (isoData).getRepresentation().getRepresentationData().getImageData().getImage(); + ImageData imageData = getIrisBDIRISO19794_6_2011 (isoData).getRepresentation().getRepresentationData().getImageData(); + try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + ImageIO.write(ImageIO.read(new ByteArrayInputStream(imageData.getImage())), "jpg", baos); + return baos.toByteArray(); + } catch (IOException e) { + LOGGER.error("Failed to get iris jpg image", e); + } + return imageData.getImage(); + } + + public static BufferedImage convertIrisISOToBufferedImage(ConvertRequestDto convertRequestDto) throws Exception + { + switch (convertRequestDto.getVersion()) { + case "ISO19794_6_2011" : + IrisBDIR irisBDIR = getIrisBDIRISO19794_6_2011 (convertRequestDto.getInputBytes()); + return ImageIO.read(new ByteArrayInputStream(irisBDIR.getRepresentation() + .getRepresentationData().getImageData().getImage())); + } + throw new UnsupportedOperationException(); } - public static byte [] convertIrisISOToImage(ConvertRequestDto convertRequestDto) throws Exception + public static byte [] convertIrisISOToImageBytes(ConvertRequestDto convertRequestDto) throws Exception { switch (convertRequestDto.getVersion()) { case "ISO19794_6_2011" : - return convertIrisISO19794_6_2011ToImage(convertRequestDto.getInputBytes()); + IrisBDIR irisBDIR = getIrisBDIRISO19794_6_2011 (convertRequestDto.getInputBytes()); + ImageFormat imageFormat = irisBDIR.getRepresentation().getRepresentationHeader().getImageInformation().getImageFormat(); + switch (imageFormat) { + case MONO_JPEG2000_LOSS_LESS: + case RGB_JPEG2000_LOSS_LESS: + return CommonUtil.convertJP2ToJPEGBytes(irisBDIR.getRepresentation() + .getRepresentationData().getImageData().getImage()); + default : + return irisBDIR.getRepresentation() + .getRepresentationData().getImageData().getImage(); + } } throw new UnsupportedOperationException(); } diff --git a/biometrics-util/src/main/java/io/mosip/biometrics/util/iris/IrisEncoder.java b/biometrics-util/src/main/java/io/mosip/biometrics/util/iris/IrisEncoder.java index d8663b366d0..be478e4c970 100644 --- a/biometrics-util/src/main/java/io/mosip/biometrics/util/iris/IrisEncoder.java +++ b/biometrics-util/src/main/java/io/mosip/biometrics/util/iris/IrisEncoder.java @@ -73,7 +73,7 @@ public class IrisEncoder { ImageType imageType = convertRequestDto.getPurpose().equalsIgnoreCase("AUTH") ? ImageType.CROPPED_AND_MASKED : ImageType.CROPPED; - ImageFormat imageFormat = ImageFormat.MONO_JPEG_LOSS_LESS;//0A + ImageFormat imageFormat = ImageFormat.MONO_JPEG2000_LOSS_LESS;//0A Orientation horizontalOrientation = Orientation.UNDEFINED; Orientation verticalOrientation = Orientation.UNDEFINED; IrisImageCompressionType compressionType = IrisImageCompressionType.UNDEFINED; diff --git a/bioutils/src/main/java/io/mosip/biometrics/util/test/BioUtilApplication.java b/bioutils/src/main/java/io/mosip/biometrics/util/test/BioUtilApplication.java index 0eb6e70e996..42db562f2a4 100644 --- a/bioutils/src/main/java/io/mosip/biometrics/util/test/BioUtilApplication.java +++ b/bioutils/src/main/java/io/mosip/biometrics/util/test/BioUtilApplication.java @@ -197,11 +197,11 @@ else if (Integer.parseInt(convertTo) == 1) //Convert Face ISO/IEC 19794-5: 2011 byte[] isoData = Files.readAllBytes(Paths.get(fileName)); requestDto.setInputBytes(isoData); - byte [] imageData = FaceDecoder.convertFaceISOToImage (requestDto); + byte [] imageData = FaceDecoder.convertFaceISOToImageBytes (requestDto); if (imageData != null) { // Write bytes to tmp file. - File tmpImageFile = new File(filePath + biometricFolderPath + converionFile + ".jp2"); + File tmpImageFile = new File(filePath + biometricFolderPath + converionFile + ".jpg"); tmpOutputStream = new FileOutputStream(tmpImageFile); tmpOutputStream.write(imageData); } @@ -248,7 +248,7 @@ public static void doIrisConversion (String purpose, String inputImageType, Stri requestDto.setBiometricSubType(biometricSubType); requestDto.setInputBytes(imageData); - byte [] isoData = IrisEncoder.convertFingerImageToISO (requestDto); + byte [] isoData = IrisEncoder.convertIrisImageToISO(requestDto); if (isoData != null) { // Write bytes to tmp file. @@ -273,12 +273,12 @@ else if (Integer.parseInt(convertTo) == 1) //Convert IRIS ISO/IEC 19794-6: 2011 LOGGER.info("doIrisConversion :: fileName ::" + fileName); byte[] isoData = Files.readAllBytes(Paths.get(fileName)); requestDto.setInputBytes(isoData); - byte [] imageData = IrisDecoder.convertIrisISOToImage (requestDto); + byte [] imageData = IrisDecoder.convertIrisISOToImageBytes (requestDto); if (imageData != null) { // Write bytes to tmp file. - File tmpImageFile = new File(filePath + biometricFolderPath + converionFile + ".jp2"); + File tmpImageFile = new File(filePath + biometricFolderPath + converionFile + ".jpg"); tmpOutputStream = new FileOutputStream(tmpImageFile); tmpOutputStream.write(imageData); } @@ -352,12 +352,12 @@ else if (Integer.parseInt(convertTo) == 1) //Convert Finger ISO/IEC 19794-4: 201 byte[] isoData = Files.readAllBytes(Paths.get(fileName)); requestDto.setInputBytes(isoData); - byte [] imageData = FingerDecoder.convertFingerISOToImage (requestDto); + byte [] imageData = FingerDecoder.convertFingerISOToImageBytes (requestDto); if (imageData != null) { // Write bytes to tmp file. if (Integer.parseInt(inputImageType) == 0) //ApplicationConstant.IMAGE_TYPE_JP2000)) - fileName = filePath + biometricFolderPath + converionFile + ".jp2"; + fileName = filePath + biometricFolderPath + converionFile + ".jpg"; else if (Integer.parseInt(inputImageType) == 1) //ApplicationConstant.IMAGE_TYPE_WSQ)) fileName = filePath + biometricFolderPath + converionFile + ".wsq"; File tmpImageFile = new File(fileName);