Java to edit multiple pictures and text into one picture

Java to edit multiple pictures and text into one picture Due to the need to generate a poster with multiple pictures and customized text in the busine...

Java to edit multiple pictures and text into one picture

Due to the need to generate a poster with multiple pictures and customized text in the business, today I wrote a public method to deal with the problem of multiple pictures splicing posters and relevant text pasting into the pictures! 1. Prepare materials -- two pictures, one background picture and one picture that needs to be covered to a certain position 2. Prepare image reorganization method 1). Set picture size Public method for setting picture size

[Java] plain text view copy code
?

public static BufferedImage resizeImage(int x,int y,BufferedImage bfi){

BufferedImage bufferedImage = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB); bufferedImage.getGraphics().drawImage( bfi.getScaledInstance(x, y, Image.SCALE_SMOOTH), 0, 0, null); return bufferedImage;

}

Set picture size in reorganization method

[Java] plain text view copy code
?

public static String overlapImage(){

try { //Set picture size BufferedImage background = resizeImage(618,1000, ImageIO.read(new File("Here is the path of the background picture!"))); BufferedImage qrCode = resizeImage(150,150,ImageIO.read(new File("Here is the path to insert the QR code picture!"))); }catch (Exception e){ e.printStackTrace(); } return null;

}

2) write a text message in a position of the background picture Draw with Graphics2D tool, write the text into the picture

[Java] plain text view copy code
?

//Add the information you need to write in the background picture, for example: welcome to use my Taobao rebate robot, a must-have at home, a special Secretary for saving money and shopping!
String message01 = "welcome to use my Taobao rebate robot";
String message02 = "it's a must at home, a special Secretary for saving money and shopping! ";
Graphics2D g = background.createGraphics();
g.setColor(Color.white);
g.setFont(new Font("Microsoft YaHei", Font.BOLD,20));
//Here, write the corresponding position of information and information
g.drawString(message01,530 ,190);
g.drawString(message02,530 ,220);
g.dispose();
ImageIO.write(background, "jpg", new File("here is the path of an output picture");

3). Insert other picture information in the middle of background picture Finally, add a QR code picture to the background

[Java] plain text view copy code
?

g.drawImage(qrCode, 700, 240, qrCode.getWidth(), qrCode.getHeight(), null);

3. Write the main method, call and view the result For easy viewing, the final code is all codes

[Java] plain text view copy code
?

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

/**

  • Created by zj on 2018/10/18.

*/
public class ImageReorganization {

public static void main(String[] args) { String backgroundPath = "D:\\test\\angelYan.jpg"; String qrCodePath = "D:\\test\\qrcode.jpg"; String message01 ="Scan the QR code below, welcome to add my Taobao rebate robot"; String message02 = "It's a must at home, a special Secretary for saving money and shopping!"; String outPutPath="D:\\test\\end.jpg"; overlapImage(backgroundPath,qrCodePath,message01,message02,outPutPath); } public static String overlapImage(String backgroundPath,String qrCodePath,String message01,String message02,String outPutPath){ try { //Set picture size

//Bufferedimage background = resizeimage (6181000, imageio. Read (new file) ("here is the path of the background picture! ())

BufferedImage background = resizeImage(1000,618, ImageIO.read(new File(backgroundPath)));

//Bufferedimage QRcode = resizeimage (150150, imageio. Read (new file ("here is the path to insert the QR code picture! ())

BufferedImage qrCode = resizeImage(150,150,ImageIO.read(new File(qrCodePath))); //Add the information to be written in the background picture, for example: scan the QR code below, welcome to add my Taobao rebate robot, necessary for home, exclusive Secretary for saving money and shopping! //String message = "scan the QR code below. Welcome to add my Taobao rebate robot. It's a must at home and a special Secretary for saving money and shopping! "; Graphics2D g = background.createGraphics(); g.setColor(Color.white); g.setFont(new Font("Microsoft YaHei",Font.BOLD,20)); g.drawString(message01,530 ,190); g.drawString(message02,530 ,220); //Add a QR code picture to the background picture g.drawImage(qrCode, 700, 240, qrCode.getWidth(), qrCode.getHeight(), null); g.dispose();

//ImageIO.write(background, "jpg", new File("here is the path of an output picture");

ImageIO.write(background, "jpg", new File(outPutPath)); }catch (Exception e){ e.printStackTrace(); } return null; } public static BufferedImage resizeImage(int x, int y, BufferedImage bfi){ BufferedImage bufferedImage = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB); bufferedImage.getGraphics().drawImage( bfi.getScaledInstance(x, y, Image.SCALE_SMOOTH), 0, 0, null); return bufferedImage; }

}

4. Final view effect Generated picture effects

6 November 2019, 23:10 | Views: 6362

Add new comment

For adding a comment, please log in
or create account

0 comments