❶ 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文本文件轉化為圖片文件怎麼弄
文件在計算機中都是以二進制保存的,但系統是以文件頭來區分各種文件格式的。
也就是說,僅僅更改後綴名是不行的。
按照你說想的,可以這么來做:
1、讀取txt文本的每一行
2、創建BufferedImage圖片,然後在圖片上畫讀取到的文本
下面給出示常式序:
測試類 TextToImageExample.java
importjava.io.File;
importjava.util.Scanner;
/**
*文本轉圖片測試類
*@authorYY29242014/11/18
*@version1.0
*/
publicclassTextToImageExample{
publicstaticvoidmain(String[]args){
Scannerin=newScanner(System.in);
System.out.print("輸入TXT文本名稱(例如:D:/java.txt):");
StringtextFileName=in.nextLine();
System.out.print("輸入保存的圖片名稱(例如:D:/java.jpg):");
StringimageFileName=in.nextLine();
TextToImageconvert=newTextToImage(newFile(textFileName),newFile(imageFileName));
booleansuccess=convert.convert();
System.out.println("文本轉圖片:"+(success?"成功":"失敗"));
}
}
文本轉圖片類 TextToImage.java
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.FileReader;
importjava.io.IOException;
importcom.sun.image.codec.jpeg.JPEGImageEncoder;
importcom.sun.image.codec.jpeg.JPEGCodec;
/**
*文本轉圖片類
*@authorYY29242014/11/18
*@version1.0
*/
publicclassTextToImage{
/**文本文件*/
privateFiletextFile;
/**圖片文件*/
privateFileimageFile;
/**圖片*/
privateBufferedImageimage;
/**圖片寬度*/
privatefinalintIMAGE_WIDTH=400;
/**圖片高度*/
privatefinalintIMAGE_HEIGHT=600;
/**圖片類型*/
privatefinalintIMAGE_TYPE=BufferedImage.TYPE_INT_RGB;
/**
*構造函數
*@paramtextFile文本文件
*@paramimageFile圖片文件
*/
publicTextToImage(FiletextFile,FileimageFile){
this.textFile=textFile;
this.imageFile=imageFile;
this.image=newBufferedImage(IMAGE_WIDTH,IMAGE_HEIGHT,IMAGE_TYPE);
}
/**
*將文本文件里文字,寫入到圖片中保存
*@returnbooleantrue,寫入成功;false,寫入失敗
*/
publicbooleanconvert(){
//讀取文本文件
BufferedReaderreader=null;
try{
reader=newBufferedReader(newFileReader(textFile));
}catch(FileNotFoundExceptione){
e.printStackTrace();
returnfalse;
}
//獲取圖像上下文
Graphicsg=createGraphics(image);
Stringline;
//圖片中文本行高
finalintY_LINEHEIGHT=15;
intlineNum=1;
try{
while((line=reader.readLine())!=null){
g.drawString(line,0,lineNum*Y_LINEHEIGHT);
lineNum++;
}
g.dispose();
//保存為jpg圖片
FileOutputStreamfos=newFileOutputStream(imageFile);
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(fos);
encoder.encode(image);
fos.close();
}catch(IOExceptione){
e.printStackTrace();
returnfalse;
}
returntrue;
}
/**
*獲取到圖像上下文
*@paramimage圖片
*@returnGraphics
*/
privateGraphicscreateGraphics(BufferedImageimage){
Graphicsg=image.createGraphics();
g.setColor(Color.WHITE);//設置背景色
g.fillRect(0,0,IMAGE_WIDTH,IMAGE_HEIGHT);//繪制背景
g.setColor(Color.BLACK);//設置前景色
g.setFont(newFont("微軟雅黑",Font.PLAIN,12));//設置字體
returng;
}
}
特別注意:程序中使用到了com.sun.image.codec.jpeg.JPEGImageEncoder和 com.sun.image.codec.jpeg.JPEGCodec ,這 兩個是sun的專用API,Eclipse會報錯。
解決辦法:
Eclipse軟體,Windows->Preferences->Java->Complicer->Errors/Warnings,Deprecated and restricted API->Forbidden reference 改為 Warnning。
如果還是報錯,在工程上build path,先移除JRE System Library,然後再添加JRE System Library。
❸ Java或js實現動態生成橢圓電子章圖片(非窗體程序)
現成寫好的印章生成小工具源碼,還支持橢圓、私章等。直通車:https://github.com/localhost02/SealUtil
❹ java在生成圖片的時候,讓文字豎排展示,如何實現
packagehonest.imageio;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjavax.imageio.ImageIO;
/**
*圖片操作類
*
*@author
*
*/
publicclassImageUtil{
privateBufferedImageimage;
privateintwidth;//圖片寬度
privateintheight;//圖片高度
publicImageUtil(intwidth,intheight){
this.width=width;
this.height=height;
image=newBufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
}
/**
*創建一個含有指定顏色字元串的圖片
*
*@parammessage
*字元串
*@paramfontSize
*字體大小
*@paramcolor
*字體顏色
*@return圖片
*/
publicBufferedImagedrawString(Stringmessage,intfontSize,Colorcolor){
Graphicsg=image.getGraphics();
g.setColor(color);
Fontf=newFont("宋體",Font.BOLD,fontSize);
g.setFont(f);
intlen=message.length();
g.drawString(message,(width-fontSize*len)/2,
(height+(int)(fontSize/1.5))/2);
g.dispose();
returnimage;
}
/**
*縮放圖片
*
*@paramscaleW
*水平縮放比例
*@paramscaleY
*垂直縮放比例
*@return
*/
publicBufferedImagescale(doublescaleW,doublescaleH){
width=(int)(width*scaleW);
height=(int)(height*scaleH);
BufferedImagenewImage=newBufferedImage(width,height,
image.getType());
Graphicsg=newImage.getGraphics();
g.drawImage(image,0,0,width,height,null);
g.dispose();
image=newImage;
returnimage;
}
/**
*旋轉90度旋轉
*
*@return對應圖片
*/
publicBufferedImagerotate(){
BufferedImagedest=newBufferedImage(height,width,
BufferedImage.TYPE_INT_ARGB);
for(inti=0;i<width;i++)
for(intj=0;j<height;j++){
dest.setRGB(height-j-1,i,image.getRGB(i,j));
}
image=dest;
returnimage;
}
/**
*合並兩個圖像
*
*@paramanotherImage
*另一張圖片
*@return合並後的圖片,如果兩張圖片尺寸不一致,則返回null
*/
publicBufferedImagemergeImage(BufferedImageanotherImage){
intw=anotherImage.getWidth();
inth=anotherImage.getHeight();
if(w!=width||h!=height){
returnnull;
}
for(inti=0;i<w;i++){
for(intj=0;j<h;j++){
intrgb1=image.getRGB(i,j);
intrgb2=anotherImage.getRGB(i,j);
Colorcolor1=newColor(rgb1);
Colorcolor2=newColor(rgb2);
//如果該位置兩張圖片均沒有字體經過,則跳過
//如果跳過,則最後將會是黑色背景
if(color1.getRed()+color1.getGreen()+color1.getBlue()
+color2.getRed()+color2.getGreen()
+color2.getBlue()==0){
continue;
}
Colorcolor=newColor(
(color1.getRed()+color2.getRed())/2,
(color1.getGreen()+color2.getGreen())/2,
(color1.getBlue()+color2.getBlue())/2);
image.setRGB(i,j,color.getRGB());
}
}
returnimage;
}
/**
*保存圖片intrgb1=image.getRGB(i,j);intrgb2=anotherImage.getRGB(i,j);
*rgb2=rgb1&rgb2;image.setRGB(height-i,j,rgb2);
*
*@paramfilePath
*圖片路徑
*/
publicvoidsave(StringfilePath){
try{
ImageIO.write(image,"png",newFile(filePath));
}catch(IOExceptione){
e.printStackTrace();
}
}
/**
*得到對應的圖片
*
*@return
*/
publicBufferedImagegetImage(){
returnimage;
}
}
❺ java中怎麼將word文檔怎麼生成圖片
public class CreateWordDemo
{
public void createDocContext(String file)
throws DocumentException,IOException {
//
設置紙張大小
Document document = new
Document(PageSize.A4);
//
建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁碟中
RtfWriter2.getInstance(document, new
FileOutputStream(file));
document.open();
//
設置中文字體
BaseFont bfChinese =
BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
//
標題字體風格
Font titleFont = new Font(bfChinese, 12,
Font.BOLD);
//
正文字體風格
Font contextFont = new Font(bfChinese, 10,
Font.NORMAL);
Paragraph title = new
Paragraph("標題");
//
設置標題格式對齊方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
document.add(title);
String contextString =
"iText是一個能夠快速產生PDF文件的java類庫。"
+ " \n"//
換行
+
"iText的java類對於那些要產生包含文本,"
+ "表格,圖形的只讀文檔是很有用的。它的類庫尤其與java
Servlet有很好的給合。"
+
"使用iText與PDF能夠使你正確的控制Servlet的輸出。";
Paragraph context = new
Paragraph(contextString);
//
正文格式左對齊
context.setAlignment(Element.ALIGN_LEFT);
context.setFont(contextFont);
//
離上一段落(標題)空的行數
context.setSpacingBefore(5);
//
設置第一行空的列數
context.setFirstLineIndent(20);
document.add(context);
//
利用類FontFactory結合Font和Color可以設置各種各樣字體樣式Paragraph underline = new Paragraph("下劃線的實現",
FontFactory.getFont(
FontFactory.HELVETICA_BOLDOBLIQUE, 18,
Font.UNDERLINE, new Color(0, 0,
255)));
document.add(underline);
// 設置 Table
表格
Table aTable = new
Table(3);
int width[] = { 25, 25, 50
};
aTable.setWidths(width);//
設置每列所佔比例
aTable.setWidth(90); // 占頁面寬度
90%
aTable.setAlignment(Element.ALIGN_CENTER);//
居中顯示
aTable.setAlignment(Element.ALIGN_MIDDLE);//
縱向居中顯示
aTable.setAutoFillEmptyCells(true); //
自動填滿
aTable.setBorderWidth(1); //
邊框寬度
aTable.setBorderColor(new Color(0, 125, 255)); //
邊框顏色
aTable.setPadding(2);//
襯距,看效果就知道什麼意思了
aTable.setSpacing(3);//
即單元格之間的間距
aTable.setBorder(2);//
邊框
//
設置表頭Cell haderCell = new
Cell("表格表頭");
haderCell.setHeader(true);
haderCell.setColspan(3);
aTable.addCell(haderCell);
aTable.endHeaders();
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL,
Color.GREEN);
Cell cell = new Cell(new Phrase("這是一個測試的 3*3 Table 數據",
fontChinese));
cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setBorderColor(new Color(255, 0,
0));
cell.setRowspan(2);
aTable.addCell(cell);
aTable.addCell(new
Cell("#1"));
aTable.addCell(new
Cell("#2"));
aTable.addCell(new
Cell("#3"));
aTable.addCell(new
Cell("#4"));
Cell cell3 = new Cell(new Phrase("一行三列數據",
fontChinese));
cell3.setColspan(3);
cell3.setVerticalAlignment(Element.ALIGN_CENTER);
aTable.addCell(cell3);
document.add(aTable);
document.add(new
Paragraph("\n"));
//
添加圖片 Image.getInstance即可以放路徑又可以放二進制位元組流
Image img =
Image.getInstance("d:\\img01800.jpg");
img.setAbsolutePosition(0,
0);
img.setAlignment(Image.RIGHT);//
設置圖片顯示位置
img.scaleAbsolute(60, 60);//
直接設定顯示尺寸
//
img.scalePercent(50);//表示顯示的大小為原尺寸的50%
// img.scalePercent(25,
12);//圖像高寬的顯示比例
//
img.setRotation(30);//圖像旋轉一定角度
document.add(img);
document.close();
}public static void main(String[] args)
{
CreateWordDemo word = new
CreateWordDemo();
String file =
"d:/demo1.doc";
try
{
word.createDocContext(file);
} catch (DocumentException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
❻ java操作字體生成png圖片,該怎麼解決
OpenGL當中有畫筆對象,可以設置字體樣式,
然後把需要的圖片,文字一一畫在畫布上,需要清楚所畫的層次,後面畫的會覆蓋前面畫的內容的,
最後把畫布內容生成一張圖片
❼ java自定義字體文字和圖片生成新圖片(高分)
這個技術好實現,思想如下:
用js控制;
再根據文字與形式生成圖片;
再輸出即可。
我以前做過。
❽ java文字轉圖片再保存到本地
看 BufferedImage 的例子。
❾ java生成jpg圖片 並且實現文字和圖片混排
response.setHeader("Cache-Control","no-cache");
String str="";
String sum="";
for(int i=0;i<4;i++){
Random random=new Random();
int j=Math.round(random.nextFloat()*35);
char x=str.charAt(j);
sum+=x+"";
}
request.getSession().setAttribute("Code",sum);
BufferedImage bufferedImage=new BufferedImage(50,20,BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics2D=(Graphics2D)bufferedImage.getGraphics();
graphics2D.setColor(Color.blue);
graphics2D.fill3DRect(0,0,50,20,false);
graphics2D.setColor(Color.YELLOW);
graphics2D.drawString(sum,10,12);
response.setContentType("image/jpeg");
ServletOutputStream output;
try {
output = response.getOutputStream();
JPEGImageEncoder encoder= JPEGCodec.createJPEGEncoder(output);
encoder.encode(bufferedImage);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}