本文目錄一覽:
- 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(” “, ” “);
s = s.replaceAll(“”, “”);
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 scaleH) {
width = (int) (width * scaleW);
height = (int) (height * scaleH);
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;
}
/**
* 保存圖片int rgb1 = image.getRGB(i, j); int rgb2 = anotherImage.getRGB(i, j);
* rgb2 = rgb1 rgb2; image.setRGB(height – i, j, rgb2);
*
* @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;
}
}
原創文章,作者:BEDCK,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/330079.html