图片操作

时间 2018/12/23 10:08:59 加载中...

图片操作 ImageTest.java

在生成 图片验证码,或者 给上传的图片 添加水印 时,会涉及到图片操作的知识。
实验见如下代码:

以下方法会引用的包有

  1. import javax.imageio.ImageIO;
  2. import java.awt.*;
  3. import java.awt.image.BufferedImage;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.Base64;
  8. import java.util.Random;

图片上写字

  1. int imgWidth = 113;
  2. int imgHeight = 45;
  3. // 创建一个画布
  4. BufferedImage bufferedImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
  5. // 创建一个画笔,要开始画东西了
  6. Graphics2D g = bufferedImage.createGraphics();
  7. // 画布背景色
  8. g.setBackground(Color.WHITE);
  9. g.clearRect(0, 0, imgWidth, imgHeight);
  10. // 画笔颜色
  11. g.setColor(Color.gray);
  12. // 画笔字体
  13. g.setFont(new Font("宋体", Font.PLAIN, imgHeight - 10));
  14. //输入汉字,必须设置字体,且 系统中必须有对应字体,否则会乱码。
  15. String content = "小明";
  16. g.drawString(content,0,imgHeight - 10);
  17. g.dispose();
  18. boolean success = false;
  19. try {
  20. File file = new File("d:/3.png");
  21. success = ImageIO.write(bufferedImage,"png",file);
  22. Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c","d:/3.png"});
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. System.out.println("result :" + success);

图片保存到磁盘,并打开

  1. BufferedImage bufferedImage = new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB);
  2. /*
  3. .........
  4. */
  5. boolean success = false;
  6. try {
  7. success = ImageIO.write(bufferedImage,"png",new File("d:/3.png"));
  8. Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c","d:/3.png"});
  9. } catch (IOException e) {
  10. e.printStackTrace();
  11. }

图片保存成base64模式

  1. ByteArrayOutputStream output = new ByteArrayOutputStream();
  2. try {
  3. ImageIO.write(bufferedImage, "png", output);
  4. output.flush();
  5. output.close();
  6. byte[] byteArr = output.toByteArray();
  7. String str = Base64.getEncoder().encodeToString(byteArr);
  8. String imgStr = new StringBuilder().append("data:image/png;base64,").append(str).toString();
  9. System.out.println(imgStr);
  10. } catch (IOException e) {
  11. e.printStackTrace();
  12. }

居中图片上的文字

  1. int imgWidth = 113;
  2. int imgHeight = 45;
  3. // 创建一个画布
  4. BufferedImage bufferedImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
  5. // 创建一个画笔,要开始画东西了
  6. Graphics2D g = bufferedImage.createGraphics();
  7. // 画布背景色
  8. g.setBackground(Color.WHITE);
  9. g.clearRect(0, 0, imgWidth, imgHeight);
  10. // 画笔颜色
  11. g.setColor(Color.gray);
  12. // 画笔字体
  13. int fontHeight = imgHeight-10;
  14. g.setFont(new Font("宋体", Font.PLAIN, fontHeight));
  15. //输入汉字,必须设置字体,且 系统中必须有对应字体,否则会乱码。
  16. String content = "小明";
  17. FontMetrics metrics = g.getFontMetrics(g.getFont());
  18. System.out.println("fontHeight:"+String.valueOf(fontHeight));
  19. System.out.println("ascent:"+metrics.getAscent());
  20. System.out.println("descent:"+metrics.getDescent());
  21. System.out.println("height:"+metrics.getHeight());
  22. int x = (imgWidth -metrics.stringWidth(content))/2;
  23. int y = ((imgHeight - metrics.getHeight()) / 2) + metrics.getAscent();
  24. g.drawString(content,x,y);
  25. g.dispose();
  26. boolean success = false;
  27. try {
  28. success = ImageIO.write(bufferedImage,"png",new File("d:/3.png"));
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. System.out.println("result :" + success);

设置图片背景色

  1. int imgWidth = 113;
  2. int imgHeight = 45;
  3. // 创建一个画布
  4. BufferedImage bufferedImage = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
  5. // 创建一个画笔,要开始画东西了
  6. Graphics2D g = bufferedImage.createGraphics();
  7. // 画布背景色
  8. g.setBackground(Color.WHITE);
  9. g.clearRect(0, 0, imgWidth, imgHeight);
  10. g.setPaint(new GradientPaint(0, 0, new Color(28, 64, 124), imgWidth, imgHeight, new Color(46, 109, 163)));
  11. // g.setPaint(Color.BLUE);
  12. g.fillRect(0, 0, imgWidth, imgHeight);
  13. g.dispose();
  14. boolean success = false;
  15. try {
  16. File file = new File("d:/3.png");
  17. success = ImageIO.write(bufferedImage,"png",file);
  18. Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c","d:/3.png"});
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }

图片上画线 多边形

  1. // 创建一个画布
  2. BufferedImage bufferedImage = new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB);
  3. // 创建一个画笔,要开始画东西了
  4. Graphics2D g = bufferedImage.createGraphics();
  5. // 画布背景色
  6. /*g.setBackground(Color.WHITE);
  7. g.clearRect(0, 0, 100,200);*/
  8. //线
  9. g.drawLine(0,0,99,120);
  10. //多边形
  11. int[] arr1 = new int[]{10,40,30,40,50};
  12. int[] arr2 = new int[]{20,40,10,10,60};
  13. g.drawPolygon(arr1,arr2,arr1.length);
  14. g.dispose();
  15. boolean success = false;
  16. try {
  17. File file = new File("d:/3.png");
  18. success = ImageIO.write(bufferedImage,"png",file);
  19. Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c","d:/3.png"});
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }

旋转角度

  1. // 创建一个画布
  2. BufferedImage bufferedImage = new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB);
  3. // 创建一个画笔,要开始画东西了
  4. Graphics2D g = bufferedImage.createGraphics();
  5. int jiaodu = 60; // 角度 [-180,180]度
  6. g.rotate(jiaodu * Math.PI / 180);
  7. g.drawString("李卫当官",30,10);
  8. g.rotate(-60 * Math.PI / 180);
  9. //平移原点:向右移动10,向下移动20
  10. g.translate(10,20);
  11. g.rotate(20 * Math.PI / 180);
  12. g.drawString("李",0,0);
  13. g.rotate(-20 * Math.PI / 180);
  14. //平移原点:向右移动20,上下不移动了
  15. g.translate(20,0);
  16. g.rotate(-20 * Math.PI / 180);
  17. g.drawString("卫",0,0);
  18. g.rotate(20 * Math.PI / 180);
  19. g.translate(20,0);
  20. g.rotate(20 * Math.PI / 180);
  21. g.drawString("当",0,0);
  22. g.rotate(-20 * Math.PI / 180);
  23. g.translate(20,0);
  24. g.rotate(-20 * Math.PI / 180);
  25. g.drawString("官",0,0);
  26. g.rotate(20 * Math.PI / 180);
  27. g.dispose();
  28. boolean success = false;
  29. try {
  30. File file = new File("d:/3.png");
  31. success = ImageIO.write(bufferedImage,"png",file);
  32. Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c","d:/3.png"});
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }

随机位置放文字

  1. public static void addOtherText(Graphics2D g, int imgWidth, int imgHeight, String text) {
  2. Random randomer = new Random();
  3. for (int i = 0; i < text.length(); i++) {
  4. char item = text.charAt(i);
  5. int pointX = randomer.nextInt(imgWidth);
  6. int pointY = randomer.nextInt(imgHeight);
  7. g.setFont(new Font("Arial", Font.PLAIN, imgHeight-18));
  8. g.drawString(Character.toString(item), pointX, pointY);
  9. }
  10. }

随机画多边形

  1. public static void addPolygon(Graphics2D g, int imgWidth, int imgHeight) {
  2. int pointNum = 10;
  3. Random randomer = new Random();
  4. int[] xArr = new int[pointNum];
  5. int[] yArr = new int[pointNum];
  6. for (int i = 0; i < pointNum; i++) {
  7. int pointX = randomer.nextInt(imgWidth);
  8. int pointY = randomer.nextInt(imgHeight);
  9. xArr[i] = pointX;
  10. yArr[i] = pointY;
  11. }
  12. g.drawPolygon(xArr, yArr, pointNum);
  13. }

随机画线

  1. public static void addLine(Graphics2D g, int imgWidth, int imgHeight) {
  2. int pointNum = 10;
  3. Random randomer = new Random();
  4. for (int i = 0; i < pointNum; i++) {
  5. int pointX1 = randomer.nextInt(imgWidth);
  6. int pointY1 = randomer.nextInt(imgHeight);
  7. int pointX2 = randomer.nextInt(imgWidth);
  8. int pointY2 = randomer.nextInt(imgHeight);
  9. g.drawLine(pointX1, pointY1, pointX2, pointY2);
  10. }
  11. }
扫码分享
版权说明
作者:SQBER
文章来源:http://www.sqber.com/articles/the-picture-related.html
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。