java生成宣传海报(h5生成海报怎么做)

发布时间:2022-11-16

本文目录一览:

  1. java怎么生成带用户微信头像的图片,并把这张图片发送给用户。
  2. java如何实现将和表格和图片画成一张图片
  3. 怎么样用Java实现将一张图片转成字符画??
  4. java在生成图片的时候,让文字竖排展示,如何实现?

java怎么生成带用户微信头像的图片,并把这张图片发送给用户。

1、下载生成二维码所需要的jar包qrcode.jar;2、直接上生成二维码的java代码:

// 需要导入的包
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
/**
 * 生成二维码(QRCode)图片
 * @param content 二维码图片的内容
 * @param imgPath 生成二维码图片完整的路径
 * @param ccbpath 二维码图片中间的logo路径
 */
public static int createQRCode(String content, String imgPath, String ccbPath) {
    try {
        Qrcode qrcodeHandler = new Qrcode();
        qrcodeHandler.setQrcodeErrorCorrect('M');
        qrcodeHandler.setQrcodeEncodeMode('B');
        qrcodeHandler.setQrcodeVersion(7);
        // System.out.println(content);
        byte[] contentBytes = content.getBytes("gb2312");
        // 构造一个BufferedImage对象 设置宽、高
        BufferedImage bufImg = new BufferedImage(140, 140, BufferedImage.TYPE_INT_RGB);
        Graphics2D gs = bufImg.createGraphics();
        gs.setBackground(Color.WHITE);
        gs.clearRect(0, 0, 140, 140);
        // 设定图像颜色 BLACK
        gs.setColor(Color.BLACK);
        // 设置偏移量 不设置可能导致解析出错
        int pixoff = 2;
        // 输出内容 二维码
        if (contentBytes.length > 0 && contentBytes.length < 120) {
            boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);
            for (int i = 0; i < codeOut.length; i++) {
                for (int j = 0; j < codeOut.length; j++) {
                    if (codeOut[j][i]) {
                        gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
                    }
                }
            }
        } else {
            System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,120 ]. ");
            return -1;
        }
        Image img = ImageIO.read(new File(ccbPath)); // 实例化一个Image对象。
        gs.drawImage(img, 55, 55, 30, 30, null);
        gs.dispose();
        bufImg.flush();
        // 生成二维码QRCode图片
        File imgFile = new File(imgPath);
        ImageIO.write(bufImg, "png", imgFile);
    } catch (Exception e) {
        e.printStackTrace();
        return -100;
    }
    return 0;
}

来自网友 孤独青鸟的博客

java如何实现将和表格和图片画成一张图片

在类里面用这个获得g对象,然后可输出到页面,我原来划过图到本地,但是图片周围有黑边。。。

BufferedImage img = new BufferedImage(宽, 高, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
ImageIO.write(img, "JPEG", response.getOutputStream()); // 到页面
ImageIO.write(img, "JPEG", 写个OutputStream流); // 到本地

怎么样用Java实现将一张图片转成字符画??

首先在D盘写一个文件"temp.html",如下内容:

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
title图片转文本/title
meta http-equiv="content-type" content="text/html; charset=gbk"
style type="text/css"
body {
font-family: 宋体; line-height: 0.8em; letter-spacing: 0px; font-size: 8px;
}
/style
/head
body
${content}
/body
/html

在D盘放一个图片(放小一点的)"a.jpg" 运行如下JAVA代码:

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.imageio.ImageIO;
public class Test {
    /** 此处设置灰度字符,此处只用十个字符,可以设置更多 */
    private static char[] cs = new char[] { '.', ',', '*', '+', '=', '', '$', '@', '#', ' ' };
    public static void main(String[] args) throws IOException {
        // 读取图片
        BufferedImage bfedimage = ImageIO.read(new File("D:\\a.jpg"));
        // 图片转字符串后的数组
        char[][] css = new char[bfedimage.getWidth()][bfedimage.getHeight()];
        for (int x = 0; x < bfedimage.getWidth(); x++) {
            for (int y = 0; y < bfedimage.getHeight(); y++) {
                int rgb = bfedimage.getRGB(x, y);
                Color c = new Color(rgb);
                // 得到灰度值
                int cc = (c.getRed() + c.getGreen() + c.getBlue()) / 3;
                css[x][y] = cs[(int) ((cc * 10 - 1) / 255)];
            }
        }
        // 取得模板HTML
        String temp = readFile(new File("D:\\temp.html"), "gbk");
        StringBuffer sb = new StringBuffer();
        // 开始拼接内容
        for (int y = 0; y < css[0].length; y++) {
            for (int x = 0; x < css.length; x++) {
                sb.append(css[x][y]);
            }
            sb.append("\r\n");
        }
        System.out.println(sb.toString());
        // 生成文件
        String content = toHTML(sb.toString());
        String filecontent = replaceStrAllNotBack(temp, "${content}", content);
        writeFile(new File("D:\\content.html"), filecontent, "gbk");
    }
    public static String toHTML(String s) {
        s = s.replaceAll("<", "<");
        s = s.replaceAll(" ", "&nbsp;");
        s = s.replaceAll(">", ">");
        s = s.replaceAll("\"", """);
        s = s.replaceAll("\\\r\\\n", "<br/>");
        s = s.replaceAll("\\\r", "<br/>");
        s = s.replaceAll("\\\n", "<br/>");
        return s;
    }
    public static String replaceStrAllNotBack(String str, String strSrc, String strDes) {
        StringBuffer sb = new StringBuffer(str);
        int index = 0;
        while ((index = sb.indexOf(strSrc, index)) != -1) {
            sb.replace(index, index + strSrc.length(), strDes);
            index += strDes.length();
        }
        return sb.toString();
    }
    /**
     * 读文件(使用默认编码)
     *
     * @param file
     * @return 文件内容
     * @throws IOException
     */
    public static String readFile(File file, String charset) throws IOException {
        InputStreamReader fr = new InputStreamReader(new FileInputStream(file), charset);
        StringBuffer sb = new StringBuffer();
        char[] bs = new char[1024];
        int i = 0;
        while ((i = fr.read(bs)) != -1) {
            sb.append(bs, 0, i);
        }
        fr.close();
        return sb.toString();
    }
    /**
     * 写文件
     *
     * @param file
     * @param string 字符串
     * @param encoding 编码
     * @return 文件大小
     * @throws IOException
     */
    public static int writeFile(File file, String string, String encoding) throws IOException {
        FileOutputStream fos = new FileOutputStream(file);
        try {
            byte[] bs = string.getBytes(encoding);
            fos.write(bs);
            return bs.length;
        } finally {
            fos.close();
        }
    }
}

打开"D:\content.html"文件看效果吧。 有什么问题可以联系我。

java在生成图片的时候,让文字竖排展示,如何实现?

package honest.imageio;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
 * 图片操作类
 *
 * @author 
 *
 */
public class ImageUtil {
    private BufferedImage image;
    private int width; // 图片宽度
    private int height; // 图片高度
    public ImageUtil(int width, int height) {
        this.width = width;
        this.height = height;
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    }
    /**
     * 创建一个含有指定颜色字符串的图片
     *
     * @param message
     *            字符串
     * @param fontSize
     *            字体大小
     * @param color
     *            字体颜色
     * @return 图片
     */
    public BufferedImage drawString(String message, int fontSize, Color color) {
        Graphics g = image.getGraphics();
        g.setColor(color);
        Font f = new Font("宋体", Font.BOLD, fontSize);
        g.setFont(f);
        int len = message.length();
        g.drawString(message, (width - fontSize * len) / 2,
                (height + (int) (fontSize / 1.5)) / 2);
        g.dispose();
        return image;
    }
    /**
     * 缩放图片
     *
     * @param scaleW
     *            水平缩放比例
     * @param scaleY
     *            垂直缩放比例
     * @return
     */
    public BufferedImage scale(double scaleW, double scaleY) {
        width = (int) (width * scaleW);
        height = (int) (height * scaleY);
        BufferedImage newImage = new BufferedImage(width, height,
                image.getType());
        Graphics g = newImage.getGraphics();
        g.drawImage(image, 0, 0, width, height, null);
        g.dispose();
        image = newImage;
        return image;
    }
    /**
     * 旋转90度旋转
     *
     * @return 对应图片
     */
    public BufferedImage rotate() {
        BufferedImage dest = new BufferedImage(height, width,
                BufferedImage.TYPE_INT_ARGB);
        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++) {
                dest.setRGB(height - j - 1, i, image.getRGB(i, j));
            }
        image = dest;
        return image;
    }
    /**
     * 合并两个图像
     *
     * @param anotherImage
     *            另一张图片
     * @return 合并后的图片,如果两张图片尺寸不一致,则返回null
     */
    public BufferedImage mergeImage(BufferedImage anotherImage) {
        int w = anotherImage.getWidth();
        int h = anotherImage.getHeight();
        if (w != width || h != height) {
            return null;
        }
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                int rgb1 = image.getRGB(i, j);
                int rgb2 = anotherImage.getRGB(i, j);
                Color color1 = new Color(rgb1);
                Color color2 = new Color(rgb2);
                // 如果该位置两张图片均没有字体经过,则跳过
                // 如果跳过,则最后将会是黑色背景
                if (color1.getRed() + color1.getGreen() + color1.getBlue()
                        + color2.getRed() + color2.getGreen()
                        + color2.getBlue() == 0) {
                    continue;
                }
                Color color = new Color(
                        (color1.getRed() + color2.getRed()) / 2,
                        (color1.getGreen() + color2.getGreen()) / 2,
                        (color1.getBlue() + color2.getBlue()) / 2);
                image.setRGB(i, j, color.getRGB());
            }
        }
        return image;
    }
    /**
     * 保存图片
     * 
     * @param filePath
     *            图片路径
     */
    public void save(String filePath) {
        try {
            ImageIO.write(image, "png", new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 得到对应的图片
     *
     * @return
     */
    public BufferedImage getImage() {
        return image;
    }
}