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)將字元串生成圖片
不明白你要干什麼,是要類似於驗證碼的功能嗎,如果是這個功能,網上的資料還是很多的,還有相關的成熟組建,比如Kaptcha。
Ⅲ java如何生成含圖片的word文檔
pageoffice很不錯,你有時間網上搜pageoffice的示例代碼參考下吧。
Ⅳ java生成pdf如何讓文字和圖片顯示在同一行
可以用表格布局
BaseFont bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", false, false, null, null);
Font fontChinese5 = new Font(bf,8);
PdfPTable table1 = new PdfPTable(2); //表格兩列
table1.setHorizontalAlignment(Element.ALIGN_CENTER); //垂直居中
table1.setWidthPercentage(100);//表格的寬度為100%
float[] wid1 ={0.75f,0.25f}; //兩列寬度的比例
table1.setWidths(wid1);
table1.getDefaultCell().setBorderWidth(0); //不顯示邊框
PdfPCell cell11 = new PdfPCell(new Paragraph("SilkRoad24 GmbH",fontChinese5)); table1.addCell(cell11);
String imagepath = "D:\\wl\\logo.png";
Image image = Image.getInstance(imagepath);
table1.addCell(image);
document.add(table1);//增加到文檔中
Ⅳ java操作字體生成png圖片,該怎麼解決
OpenGL當中有畫筆對象,可以設置字體樣式,
然後把需要的圖片,文字一一畫在畫布上,需要清楚所畫的層次,後面畫的會覆蓋前面畫的內容的,
最後把畫布內容生成一張圖片
Ⅵ 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();
}
Ⅶ 如何利用java在圖片上添加文字
// 讀取模板圖片內容
BufferedImage image = ImageIO.read(new FileInputStream("c:\\test.jpg"));
Graphics2D g = image.createGraphics();// 得到圖形上下文
g.setColor(Color.BLACK); // 設置畫筆顏色
// 設置字體
g.setFont(new Font("宋體", Font.LAYOUT_LEFT_TO_RIGHT, 15));// 寫入簽名
// 下面這一句中的43,image.getHeight()-10可以改成你要的坐標。
g.drawString("這是新加入的文字", 43, image.getHeight() - 10);
g.dispose();
FileOutputStream out = new FileOutputStream("c:\\test1.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
希望我的回答能幫助你 謝謝 呵呵 答案補充 肯定了 在一張紙上寫字 肯定會自動換行的 否則寫外面?邏輯都通不過去~ 答案補充 把你的意思說出了 是不是隨便的寫字然後就提交 就把字寫在了圖片上還是?多少字 有沒有約束? 答案補充 g.drawString("這是新加入的文字", 43, image.getHeight() - 10);
在這個做處理 你規定了字體的大小 那麼獲取字元的長度*大小 一次判斷不要大於寬度 否則image.getHeight() - 10++ 你試試 應該可以的 我這里沒有Eclipse 答案補充 public class mains {
/**
* @param args
*/
private static int fontsize = 15;
static String jj(String str)
{
String sContent = str;
sContent=sContent.replaceAll(" "," ");
sContent=sContent.replaceAll("<br/>","/");
sContent=sContent.replaceAll("<br>","/");
return sContent;
} 答案補充 static void hh(String str)
{
BufferedImage image;
try {
image = ImageIO.read(new FileInputStream("E:\\dian zi za /789.jpg"));
Graphics2D g = image.createGraphics();// 得到圖形上下文
g.setColor(Color.BLACK); // 設置畫筆顏色
// 設置字體
g.setFont(new Font("宋體", Font.LAYOUT_LEFT_TO_RIGHT, fontsize));// 寫入簽名
// 下面這一句中的43,image.getHeight()-10可以改成你要的坐標。
String text = jj(str); 答案補充 String [] text1 = text.split("/");
int h = image.getHeight();
int w = image.getWidth();
for(int i=0;i<text1.length;i++)
{
g.drawString(text1[i],0,fontsize+i*fontsize);
}
g.dispose();
FileOutputStream out = new FileOutputStream("E:\\dian zi za /789.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close(); 答案補充 public static void main(String[] args) {
// TODO Auto-generated method stub
hh("sdakljsdhww<br/>asdasd<br/>a");
}
}
試試 可以換行 答案補充 關於 圖片的尺寸以及字體是否超出 你自己試著解決下 不要過於依賴問
Ⅷ 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自定義字體文字和圖片生成新圖片(高分)
這個技術好實現,思想如下:
用js控制;
再根據文字與形式生成圖片;
再輸出即可。
我以前做過。