Implementation of Java Operation Transparent Picture and Keeping Background Transparent

  • 2021-12-11 18:05:59
  • OfStack

Directory Java operates transparent pictures and keeps background transparent Java picture background transparent and transparency processing

Java manipulates transparent pictures and keeps background transparent

The most recent requirement is to add Chinese characters to the picture with transparent background. After adding Chinese characters, the background will turn black. Attach a solution


public static void pressText2(String sourceImg,String targetImg) {
    try {
        File file = new File(sourceImg);
        File targetfile = new File(targetImg);
        Image image = ImageIO.read(file);
        int width = image.getWidth(null);
        int height = image.getHeight(null);
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bufferedImage.createGraphics();
        bufferedImage = g.getDeviceConfiguration()
                .createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        g = bufferedImage.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.setColor(Color.BLACK);
        g.drawString("wudididi",20,20);
        ImageIO.write(bufferedImage, "png", targetfile);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

In this way, the new picture will have added Chinese characters and keep the background transparent

Java Image Background Transparency and Transparency Processing

For example, the following is the background transparency and transparency processing for pictures realized through java for your reference when you need it:


/**
*  Set the source picture to background transparency and set transparency 
* @param srcFile  Source picture 
* @param desFile  Object file 
* @param alpha  Transparency 
* @param formatName  File format 
* @throws IOException
*/
public static void transparentImage(String srcFile,String desFile,int alpha,String formatName) throws IOException{
    BufferedImage temp =  ImageIO.read(new File(srcFile));// Get a picture 
        transparentImage(temp, desFile, alpha, formatName);
    }
/**
*  Set the source picture to background transparency and set transparency 
* @param srcImage  Source picture 
* @param desFile  Object file 
* @param alpha  Transparency 
* @param formatName  File format 
* @throws IOException
*/
public static void transparentImage(BufferedImage srcImage,
String desFile, int alpha, String formatName) throws IOException {
int imgHeight = srcImage.getHeight();// Get the length and width of the picture 
        int imgWidth = srcImage.getWidth();
        int c = srcImage.getRGB(3, 3);
        // Prevent offside 
        if (alpha < 0) {
            alpha = 0;
         } else if (alpha > 10) {
            alpha = 10;
         }
        BufferedImage bi = new BufferedImage(imgWidth, imgHeight,
                BufferedImage.TYPE_4BYTE_ABGR);// New 1 Supports transparent BufferedImage
        for(int i = 0; i < imgWidth; ++i)// Copy the contents of the original picture to the new picture and set the background to transparent 
        {
            for(int j = 0; j < imgHeight; ++j)
            {
            // Make the background transparent 
                if(srcImage.getRGB(i, j) == c){
                    bi.setRGB(i, j, c & 0x00ffffff);
                }
                // Setting Transparency 
                else{
                int rgb = bi.getRGB(i, j);
                    rgb = ((alpha * 255 / 10) << 24) | (rgb & 0x00ffffff);
                    bi.setRGB(i, j, rgb);
                }
            }
        }
ImageIO.write(bi, StringUtils.isEmpty(formatName)?FORMAT_PNG:formatName, new File(desFile));
}

Related articles: