Java生成并合并图片以及base64转图片

时间:2022-08-25 16:22:26

由于最近研究了下打码平台(验证码识别平台),但是像需要特殊说明的验证码(例如:只需要提取验证码中黄颜色的字符),就需要将说明

信息和验证码图片合成一张图片,然后在发送给打码平台才可以。所以,我就研究了下使用Java操作图片。

因为我获取到的验证码图片信息是经过base64编码的字符串,所以我需要先进行解码。其次因为我只需要提取特定颜色的信息,所以我

还要将具体信息和验证码进行图片合成。

下面就来看一下我写的DEMO:

import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

public class ImageOperation {

public static final String diskPath = "E:/TEMP/IMAGE/"; // 图片临时文件路径

public static final Map<String, Color> colorMap = new HashMap<String,Color>();
public static final Map<String, String> tipsMap = new HashMap<String,String>();
static{
colorMap.put("00", Color.BLACK);
colorMap.put("01", Color.RED);
colorMap.put("02", Color.YELLOW);
colorMap.put("03", Color.BLUE);
tipsMap.put("00", "选择验证码");
tipsMap.put("01", "选择红色验证码");
tipsMap.put("02", "选择黄色验证码");
tipsMap.put("03", "选择蓝色验证码");
}


public static void main(String[] args) throws Exception {
long startTime = System.nanoTime();
createFolder(diskPath);
String imageBase64 = "iVBORw0KGgoAAAANSUhEUgAAAFoAAAAjCAIAAACb54pcAAAKXUlEQVR42tVaCThUexs/yRJjX0t81pS0pw2pq2QJhYjbQijVVcgnUW5SIVIUkYomynap3CJR6VqzRJFJEbcSZcluhNx3nLmHxhgzsnzf8/yf87zn/e+/83t/7/+cGYRYUEZPETVcjhpPXBro7DIppSmT/We6I+O0rPQLfzBUYubrM9plPMrYwHFM8BFc5U7YjgKISSlnWKeNIxyjY8SIJWIK3/8lOyZ40dtuOI7haHaqTAzAscczbqjTdI34mKOQFRz+M92bN7fRqN208uUY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onCc3ga89Bm/wCHa/l8eRGk8QAAAABJRU5ErkJggg==";
String yzmImagePath = encode64Image(imageBase64);
String tipsImagePath = generateImageTips(tipsMap.get("01"),colorMap.get("01"));
String composeImagePath = composeImage(tipsImagePath, yzmImagePath);
deleteFile(new String[]{yzmImagePath,tipsImagePath});
System.out.println("共消耗"+((System.nanoTime()-startTime)/1000000000)+"秒");
}

// 生成文件路径
public static boolean createFolder(String folderPath){
if(StringUtils.isBlank(folderPath)){
return false;
}
File file = new File(folderPath);
if(!file.exists()){
file.mkdirs();
}
return true;
}



// 解码base64图片,并保存
public static String encode64Image(String imageBase64){

if(StringUtils.isBlank(imageBase64)){
return null;
}
String fileName = getRandomString(15);
byte[] decode = Base64.decodeBase64(imageBase64);
int length = decode.length;
for(int i=0;i<length;++i){
if(decode[i] < 0){
decode[i] += 256;
}
}
OutputStream out = null;
try {
out = new FileOutputStream(new File(diskPath+fileName));
try {
out.write(decode);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return diskPath+fileName;

}

// 生成提示文字图片
public static String generateImageTips(String tips,Color color) throws Exception{
int width = 90;
int height = 35;
String fileName = getRandomString(15);
File file = new File(diskPath+fileName);

Font font = new Font("Serif", Font.BOLD, 10);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)bi.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.setPaint(color);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(tips, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;

g2.drawString(tips, (int)x, (int)baseY);
ImageIO.write(bi, "png", file);

return diskPath+fileName;
}

// 合成图片
public static String composeImage(String img1Path,String img2Path){
try {
//第一张图片
File fileOne = new File(img1Path);
BufferedImage imageOne = ImageIO.read(fileOne);
int width = imageOne.getWidth();
int height = imageOne.getHeight();
int[] imageArrayOne = new int[width*height];
imageArrayOne = imageOne.getRGB(0,0,width,height,imageArrayOne,0,width);

//第二张图片
File fileTwo = new File(img2Path);
BufferedImage imageTwo = ImageIO.read(fileTwo);
int width2 = imageTwo.getWidth();
int height2 = imageTwo.getHeight();
int[] ImageArrayTwo = new int[width2*height2];
ImageArrayTwo = imageTwo.getRGB(0,0,width,height,ImageArrayTwo,0,width);

//新图片
BufferedImage imageNew = new BufferedImage(width,height*2,BufferedImage.TYPE_INT_RGB);
imageNew.setRGB(0,0,width,height,imageArrayOne,0,width);
imageNew.setRGB(0,height,width,height,ImageArrayTwo,0,width);

String fileName = getRandomString(15);

File outFile = new File(diskPath+fileName+".png");
ImageIO.write(imageNew, "png", outFile);

return diskPath+fileName+".png";
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

// 删除无用的图片
public static boolean deleteFile(String[] paths){
if(paths == null || paths.length == 0){
return false;
}
File file = null;
for(String p:paths){
file = new File(p);
file.deleteOnExit();
}
return true;
}

// 生成图片临时文件名
public static String getRandomString(int length) { //length表示生成字符串的长度
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}

}

上述代码可直接使用。