Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
English

You can create an environment variable with the following code below, which can be used later with Bean Shell Hash Variable to convert the data to image base64bytes source.

As an example, we will create a new environment variable with the id "generateImage", while assuming that the table name is "table1" and signature field id is "field1".

Then, copy and paste the code below as the value of the "generateImage" environment variable.

Thai

คุณสามารถสร้างตัวแปรสภาพแวดล้อมด้วยรหัสต่อไปนี้ด้านล่างซึ่งสามารถใช้ในภายหลังกับ Bean Shell Hash Variable เพื่อแปลงข้อมูลเป็นอิมเมจฐาน 64 ไบต์

ตัวอย่างเช่นเราจะสร้างตัวแปรสภาพแวดล้อมใหม่ด้วย id "generateImage" ในขณะที่สมมติว่าชื่อตารางคือ "table1" และรหัสเขตข้อมูลลายเซ็นคือ "field1"

จากนั้นคัดลอกและวางรหัสด้านล่างเป็นค่าของตัวแปรสภาพแวดล้อม "generateImage"

Code Block
languagejava
linenumberstrue
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import org.json.JSONArray;
import org.json.JSONObject;
import javax.imageio.ImageIO;
import orgjava.apache.axis.encoding.Base64net.URLDecoder;
import java.netutil.URLDecoderBase64;

//Here defines the signature image size
int width = 200;
int height = 80;

try	{
    String jsonstr = URLDecoder.decode(json[0], "UTF-8");
    JSONArray jarr = new JSONArray(jsonstr);
    BufferedImage offscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = offscreenImage.createGraphics();
    g2.setColor(Color.white);
    g2.fillRect(0,0,width,height);
    g2.setPaint(Color.black);
    g2.setStroke(new BasicStroke(2));
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    for (int i = 0; i < jarr.length(); i++) {
        JSONObject jobj = jarr.get(i);
        g2.drawLine(jobj.getInt("lx"), jobj.getInt("ly"), jobj.getInt("mx"), jobj.getInt("my"));
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write( offscreenImage, "png", baos );
    baos.flush();
    byte[] imageInByte = baos.toByteArray();
    baos.close();
     
	String base64bytes = Base64.getEncoder().encodeencodeToString(imageInByte);
     String String src = "data:image/png;base64," + base64bytes;

    return src;
} catch (Exception e) { }  

After that, you may utilize Bean Shell Hash Variable to make use of this environment variable in your email content to display the signature as an image.

...

Code Block
<img src="#beanshell.generateImage[json={form.table1.field1?url}]#" />

Download Related Plugin

Related Elements

Thai

องค์ประกอบที่เกี่ยวข้อง