0
javaを使用してローカルストレージの作業を行わずにQRcodeを電子メールで直接送信できますか?以下のコードはQRcodeを生成します。javaを使用して電子メールにQRコードを送信
public static void createQRCode(String qrCodeData, String filePath,
String charset, Map hintMap, int qrCodeheight, int qrCodewidth)
throws WriterException, IOException {
BitMatrix matrix = new MultiFormatWriter().encode(
new String(qrCodeData.getBytes(charset), charset),
BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight, hintMap);
MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath
.lastIndexOf('.') + 1), new File(filePath));
System.out.println("QR code encoded");
}
public static String readQRCode(String filePath, String charset, Map hintMap)
throws FileNotFoundException, IOException, NotFoundException {
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(
ImageIO.read(new FileInputStream(filePath)))));
Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap,
hintMap);
return qrCodeResult.getText();
}
上記の生成されたQRをボディセクションの下のコードにローカルに保存せずに添付します。私は日食火星を使用しています。..
String from = USER_NAME;
String pass = PASSWORD;
String[] to = {RECIPIENT};
String subject = "Java mail example";
String body= "hi this is tetsting mail!";
Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for(int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
for(int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(subject);
message.setText(body, "UTF-8", "html");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("Email Sent successfully");
}
catch (AddressException ae) {
ae.printStackTrace();
}
catch (MessagingException me) {
me.printStackTrace();
}
}
チェックのような何か他のものを使用し、このうち:https://www.tutorialspoint.com/javamail_api/javamail_api_send_inlineimage_in_email.htm –
イメージとして、このQRコードを保存して – xsami
ちょうど使用した電子メールとして送信するようにしてください#writeToStreamはファイルhttps://zxing.github.io/zxing/apidocs/com/google/zxing/client/jのです2se/MatrixToImageWriter.html#writeToStream-com.google.zxing.common.BitMatrix-java.lang.String-java.io.OutputStream- – Antoniossss