Skip to content

Commit

Permalink
Merge pull request mosip#8 from ase-101/develop
Browse files Browse the repository at this point in the history
convert image to displayable image
  • Loading branch information
JanardhanBS-SyncByte authored Jun 18, 2021
2 parents 3cb6a26 + 4fbb19e commit 81a82f7
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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()) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 81a82f7

Please sign in to comment.