private void doWrite(OutputStream os, Path path) throws IOException {
try {
Writer writer = new MultiFormatWriter();
Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name());
hints.put(EncodeHintType.MARGIN, Integer.valueOf(margin));
hints.put(EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.forBits(errorCorrectionLevel.getBits()));
BitMatrix matrix = writer.encode(uri.toUriString(), BarcodeFormat.QR_CODE, width, height, hints);
if (os != null) {
MatrixToImageWriter.writeToStream(matrix, imageFormatName, os);
}
else {
MatrixToImageWriter.writeToPath(matrix, imageFormatName, path);
}
} catch (WriterException e) {
throw new IOException(e);
}
}