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控制;
再根据文字与形式生成图片;
再输出即可。
我以前做过。