Code with Finding: |
class PassportService {
/**
* Reads the face of the passport holder.
*
* @return the first face found on the passport
*
* @throws IOException if something goes wrong
*/
public BufferedImage readFace() throws IOException {
byte[] tag = { 0x5F, 0x2E };
byte[] facialRecordData = (byte[])readObject(PassportFileService.EF_DG2, tag).getValueAsBytes();
if (facialRecordData == null) {
System.out.println("DEBUG: facialRecordData == null");
}
DataInputStream in = new DataInputStream(new ByteArrayInputStream(facialRecordData));
/* Facial Record Header (14) */
in.skip(4); // 'F', 'A', 'C', 0
in.skip(4); // version in ascii (e.g. "010")
long length = in.readInt() & 0x000000FFFFFFFFL;
int faceCount = in.readUnsignedShort();
for (int face = 0; face < faceCount; face++) {
/* Facial Information (20) */
long faceImageBlockLength = in.readInt() & 0x00000000FFFFFFFFL;
int featurePointCount = in.readUnsignedShort();
byte gender = in.readByte();
byte eyeColor = in.readByte();
byte hairColor = in.readByte();
long featureMask = in.readUnsignedByte();
featureMask = (featureMask << 16) | in.readUnsignedShort();
short expression = in.readShort();
long poseAngle = in.readUnsignedByte();
poseAngle = (poseAngle << 16) | in.readUnsignedShort();
long poseAngleUncertainty = in.readUnsignedByte();
poseAngleUncertainty = (poseAngleUncertainty << 16) | in.readUnsignedShort();
/* Feature Point(s) (optional) (8 * featurePointCount) */
for (int i = 0; i < featurePointCount; i++) {
byte featureType = in.readByte();
byte featurePoint = in.readByte();
int x = in.readUnsignedShort();
int y = in.readUnsignedShort();
in.skip(2); // 2 bytes reserved
}
/* Image Information */
byte faceImageType = in.readByte();
byte imageDataType = in.readByte();
int width = in.readUnsignedShort();
int height = in.readUnsignedShort();
byte imageColorSpace = in.readByte();
int sourceType = in.readUnsignedByte();
int deviceType = in.readUnsignedShort();
int quality = in.readUnsignedShort();
/* Read JPEG2000 data */
ImageInputStream iis = ImageIO.createImageInputStream(in);
Iterator readers = ImageIO.getImageReadersByMIMEType("image/jpeg2000");
if (!readers.hasNext()) {
throw new IllegalStateException("No jpeg 2000 readers");
}
ImageReader reader = (ImageReader)readers.next();
reader.setInput(iis);
ImageReadParam pm = reader.getDefaultReadParam();
pm.setSourceRegion(new Rectangle(0, 0, width, height));
BufferedImage image = reader.read(0, pm);
return image; // FIXME: return all images, instead of only the first one :)
}
return null;
}
}
|