❶ 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();
}