encodeMessageIntoImage function
Implementation
EncodeResponse encodeMessageIntoImage(EncodeRequest req) {
Uint16List img = Uint16List.fromList(req.original.getBytes().toList());
String msg = req.msg;
String token = req.token;
if (req.shouldEncrypt()) {
crypto.Key key = crypto.Key.fromUtf8(padCryptionKey(token));
crypto.IV iv = crypto.IV.fromLength(16);
crypto.Encrypter encrypter = crypto.Encrypter(crypto.AES(key));
crypto.Encrypted encrypted = encrypter.encrypt(msg, iv: iv);
msg = encrypted.base64;
}
Uint16List encodedImg = img;
if (getEncoderCapacity(img) < getMsgSize(msg)) {
throw FlutterError('image_capacity_not_enough');
}
Uint16List expandedMsg = expandMsg(msg2bytes(msg));
Uint16List paddedMsg = padMsg(getEncoderCapacity(img), expandedMsg);
if (paddedMsg.length != getEncoderCapacity(img)) {
throw FlutterError('msg_container_size_not_match');
}
for (int i = 0; i < getEncoderCapacity(img); ++i) {
encodedImg[i] = encodeOnePixel(img[i], paddedMsg[i]);
}
imglib.Image editableImage = imglib.Image.fromBytes(
req.original.width, req.original.height, encodedImg.toList());
Image displayableImage =
Image.memory(imglib.encodePng(editableImage), fit: BoxFit.fitWidth);
Uint8List data = Uint8List.fromList(imglib.encodePng(editableImage));
EncodeResponse response =
EncodeResponse(editableImage, displayableImage, data);
return response;
}