① 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怎麼生成帶用戶微信頭像的圖片,並把這張圖片發送給用戶。
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中導出圖片怎麼做
package com.xolt;
import java.io.FileOutputStream;
import java.io.File;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;;
public class TestPOI {
public static void main(String[] args) {
FileOutputStream fileOut = null;
BufferedImage bufferImg =null;
BufferedImage bufferImg1 = null;
try{
//先把讀進來的圖片放到一個ByteArrayOutputStream中,以便產生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("C:/Documents and Settings/dingqi/Desktop/clip_image002.jpg"));
bufferImg1 = ImageIO.read(new File("C:/Documents and Settings/dingqi/Desktop/clip_image002.jpg"));
ImageIO.write(bufferImg,"jpg",byteArrayOut);
ImageIO.write(bufferImg1,"jpg",byteArrayOut1);
//創建一個工作薄
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("poi picT");
//HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 1,1,(short)10,20);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 2,30,(short)10,60);
anchor1.setAnchorType(2);
//插入圖片
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut = new FileOutputStream("c:/workbook.xls");
//寫入excel文件
wb.write(fileOut);
fileOut.close();
}catch(IOException io){
io.printStackTrace();
System.out.println("io erorr : "+ io.getMessage());
} finally
{
if (fileOut != null)
{
try {
fileOut.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
poi中圖片到到excel的方法 你需要准備poi包 試試看看