Skip to content
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

[MOSIP-29095] #10

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@

public class ImageCompressionService extends SDKService {
private Logger LOGGER = LoggerFactory.getLogger(ImageCompressionService.class);

static {
// load OpenCV library
nu.pattern.OpenCV.loadShared();
/**
* In Java >= 12 it is no longer possible to use addLibraryPath, which modifies the
* ClassLoader's static usr_paths field. There does not seem to be any way around this
* so we fall back to loadLocally() and return.
*/
//nu.pattern.OpenCV.loadLocally();
* In Java >= 12 it is no longer possible to use addLibraryPath, which modifies
* the ClassLoader's static usr_paths field. There does not seem to be any way
* around this so we fall back to loadLocally() and return.
*/
// nu.pattern.OpenCV.loadLocally();
System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
}

private BiometricRecord sample;
private List<BiometricType> modalitiesToExtract;

Expand All @@ -61,8 +61,8 @@ public ImageCompressionService(Environment env, BiometricRecord sample, List<Bio
this.modalitiesToExtract = modalitiesToExtract;
}

public Response<BiometricRecord> getExtractTemplateInfo() {
LOGGER.info("ExtractTemplateInfo :: Started Request :: " + sample != null ? sample.toString() : null);
public Response<BiometricRecord> getExtractTemplateInfo() {
LOGGER.info("ExtractTemplateInfo :: Started Request :: {}", sample != null ? sample.toString() : null);

ResponseStatus responseStatus = null;
Response<BiometricRecord> response = new Response<>();
Expand All @@ -79,26 +79,28 @@ public Response<BiometricRecord> getExtractTemplateInfo() {
byte[] faceBdb = getBirData(segment);

/*
* Below Code can be removed if we require PayLoad information
* Below Code can be removed if we require PayLoad information
*/
/*
if (segment.getBirInfo() == null)
segment.setBirInfo(new BIRInfo(new BIRInfoBuilder().withPayload(segment.getBdb())));
else
segment.getBirInfo().setPayload(segment.getBdb());
*/

* if (segment.getBirInfo() == null) segment.setBirInfo(new BIRInfo(new
* BIRInfoBuilder().withPayload(segment.getBdb()))); else
* segment.getBirInfo().setPayload(segment.getBdb());
*/

BDBInfo bdbInfo = segment.getBdbInfo();
if (bdbInfo != null) {
// Update the level to processed
// Update the level to RAW
bdbInfo.setLevel(getProcessedLevelType());
if (segment.getBdbInfo().getFormat() != null) {
String type = segment.getBdbInfo().getFormat().getType();
// Update the fingerprint image to fingerprint minutiae type
if (type != null && type.equals(String.valueOf(FORMAT_TYPE_FACE))) {
// do actual resize and compression .. create the face ISO ISO19794_5_2011
byte[] data = doFaceConversion("REGISTRATION", resizeAndCompress(faceBdb));
segment.setBdb(Base64.getEncoder().encode(data));
segment.setBdb(data);
} else {
throw new SDKException(ResponseStatus.INVALID_INPUT.toString(),
String.format(" FORMAT_TYPE_FACE is wrong ! Excepected Value is 8, Received is %s", type));
}
}
}
Expand Down Expand Up @@ -154,15 +156,16 @@ public Response<BiometricRecord> getExtractTemplateInfo() {
response.setStatusCode(ResponseStatus.SUCCESS.getStatusCode());
response.setResponse(sample);

LOGGER.info("ExtractTemplateInfo :: End Response :: ", response != null ? response.toString() : null);
LOGGER.info("ExtractTemplateInfo :: End Response :: {}", response != null ? response.toString() : null);
return response;
}

public byte[] resizeAndCompress(byte[] jp2000Bytes) throws IOException {
// Storing the image in a Matrix object
// of Mat type
Mat src = Imgcodecs.imdecode(new MatOfByte(jp2000Bytes), Imgcodecs.IMREAD_UNCHANGED);
LOGGER.info("Orginal Image Details :: Width {} Height {} Total Size {}", src.width(), src.height(), (src.width() * src.height()));
LOGGER.info("Orginal Image Details :: Width {} Height {} Total Size {}", src.width(), src.height(),
(src.width() * src.height()));
// New matrix to store the final image
// where the input image is supposed to be written
Mat dst = new Mat();
Expand All @@ -172,23 +175,24 @@ public byte[] resizeAndCompress(byte[] jp2000Bytes) throws IOException {
float fxOrginal = 0.25f;
float fyOrginal = 0.25f;
int compression = 50;
if (this.getEnv() != null)
{
if (this.getEnv() != null) {
fxOrginal = this.getEnv().getProperty(SdkConstant.IMAGE_COMPRESSOR_RESIZE_FACTOR_FX, Float.class, 0.25f);
fyOrginal = this.getEnv().getProperty(SdkConstant.IMAGE_COMPRESSOR_RESIZE_FACTOR_FY, Float.class, 0.25f);
compression = this.getEnv().getProperty(SdkConstant.IMAGE_COMPRESSOR_COMPRESSION_RATIO, Integer.class, 50);
}

LOGGER.info("Factor ratio Details :: {} ", String.format("orginal fx=%.2f, orginal fy=%.2f, Compression Ratio==%d ", fxOrginal, fyOrginal, compression));

LOGGER.info("Factor ratio Details :: {} ", String
.format("orginal fx=%.2f, orginal fy=%.2f, Compression Ratio==%d ", fxOrginal, fyOrginal, compression));

Imgproc.resize(src, dst, new Size(0, 0), fxOrginal, fyOrginal, Imgproc.INTER_AREA);
LOGGER.info("Resized Image Details :: Width {} Height {} Total Size {}", dst.width(), dst.height(), (dst.width() * dst.height()));
LOGGER.info("Resized Image Details :: Width {} Height {} Total Size {}", dst.width(), dst.height(),
(dst.width() * dst.height()));

MatOfInt map = new MatOfInt(Imgcodecs.IMWRITE_JPEG2000_COMPRESSION_X1000, compression);
MatOfByte mem = new MatOfByte();
Imgcodecs.imencode(".jp2", dst, mem, map);
byte[] data = mem.toArray();

LOGGER.info("Compressed Image Details :: Image length {}", data.length);

return data;
Expand All @@ -204,7 +208,7 @@ public byte[] doFaceConversion(String purpose, byte[] imageData) {

// Convert JP2000 to Face ISO/IEC 19794-5: 2011
if (imageData != null) {
requestDto.setImageType(0);//0 = jp2, 1 = wsq
requestDto.setImageType(0);// 0 = jp2, 1 = wsq
requestDto.setInputBytes(imageData);

// get image quality = 40 by default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ public void test_face() {
if (response != null && response.getResponse() != null)
{
BiometricRecord compressed_record = response.getResponse();
System.out.println("==================Response==================");
System.out.println(compressed_record.toString());
LOGGER.info("Response {}", compressed_record.toString());

Assert.assertEquals("Should be Intermediate", compressed_record.getSegments().get(0).getBdbInfo().getLevel().toString(), ProcessedLevelType.RAW.toString());
Assert.assertEquals("Should be Raw", compressed_record.getSegments().get(0).getBdbInfo().getLevel().toString(), ProcessedLevelType.RAW.toString());

LOGGER.info("BDB base64 encoded {}", Base64.getEncoder().encodeToString(compressed_record.getSegments().get(0).getBdb()));
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import io.mosip.kernel.biometrics.model.Response;

public class SampleSDKTest1 {
Logger LOGGER = LoggerFactory.getLogger(SampleSDKTest.class);
Logger LOGGER = LoggerFactory.getLogger(SampleSDKTest1.class);

private static String sampleFace = "";

Expand All @@ -44,7 +44,7 @@ public static void main(String[] args) {
}

public void Setup() {
sampleFace = SampleSDKTest.class.getResource("/sample_files/sample_face.xml").getPath();
sampleFace = SampleSDKTest1.class.getResource("/sample_files/sample_face.xml").getPath();
}

public void test_face() {
Expand All @@ -61,7 +61,7 @@ public void test_face() {
if (response != null && response.getResponse() != null)
{
BiometricRecord compressed_record = response.getResponse();
Assert.assertEquals("Should be Intermediate", compressed_record.getSegments().get(0).getBdbInfo().getLevel().toString(), ProcessedLevelType.RAW.toString());
Assert.assertEquals("Should be Raw", compressed_record.getSegments().get(0).getBdbInfo().getLevel().toString(), ProcessedLevelType.RAW.toString());
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
Expand Down
Loading