java生成圖片,java生成圖片表格自動換行

本文目錄一覽:

JAVA怎樣將PPTX文件轉換成圖片

關鍵代碼就是: rtruns[l].setFontName(“宋體”);

import java.awt.Dimension;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics2D;

import java.awt.geom.Rectangle2D;

import java.awt.image.BufferedImage;

import org.apache.poi.hslf.model.TextRun;

import org.apache.poi.hslf.record.Slide;

import org.apache.poi.hslf.usermodel.RichTextRun;

import org.apache.poi.hslf.usermodel.SlideShow;

public class PPTtoImage {

public static void main(String[] args) {

// 讀入PPT文件

File file = new File(“F:/20110618.ppt”);

doPPTtoImage(file);

}

public static boolean doPPTtoImage(File file) {

boolean isppt = checkFile(file);

if (!isppt) {

System.out.println(“The image you specify don’t exit!”);

return false;

}

try {

FileInputStream is = new FileInputStream(file);

SlideShow ppt = new SlideShow(is);

is.close();

Dimension pgsize = ppt.getPageSize();

org.apache.poi.hslf.model.Slide[] slide = ppt.getSlides();

for (int i = 0; i slide.length; i++) {

System.out.print(“第” + i + “頁。”);

TextRun[] truns = slide[i].getTextRuns();

for (int k = 0; k truns.length; k++) {

RichTextRun[] rtruns = truns[k].getRichTextRuns();

for (int l = 0; l rtruns.length; l++) {

int index = rtruns[l].getFontIndex();

String name = rtruns[l].getFontName();

rtruns[l].setFontIndex(1);

rtruns[l].setFontName(“宋體”);

System.out.println(rtruns[l].getText());

}

}

BufferedImage img = new BufferedImage(pgsize.width,

pgsize.height, BufferedImage.TYPE_INT_RGB);

Graphics2D graphics = img.createGraphics();

graphics.setPaint(Color.white);

graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,

pgsize.height));

slide[i].draw(graphics);

// 這裡設置圖片的存放路徑和圖片的格式(jpeg,png,bmp等等),注意生成文件路徑

FileOutputStream out = new FileOutputStream(“F:/test/pict_”

+ (i + 1) + “.jpeg”);

javax.imageio.ImageIO.write(img, “jpeg”, out);

out.close();

}

System.out.println(“success!!”);

return true;

} catch (FileNotFoundException e) {

System.out.println(e);

// System.out.println(“Can’t find the image!”);

} catch (IOException e) {

}

return false;

}

// function 檢查文件是否為PPT

public static boolean checkFile(File file) {

boolean isppt = false;

String filename = file.getName();

String suffixname = null;

if (filename != null filename.indexOf(“.”) != -1) {

suffixname = filename.substring(filename.indexOf(“.”));

if (suffixname.equals(“.ppt”)) {

isppt = true;

}

return isppt;

} else {

return isppt;

}

}

}

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 honest.imageio;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

/**

 * 圖片操作類

 * 

 * @author 

 * 

 */

public class ImageUtil {

    private BufferedImage image;

    private int width; // 圖片寬度

    private int height; // 圖片高度

    public ImageUtil(int width, int height) {

        this.width = width;

        this.height = height;

        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    }

    /**

     * 創建一個含有指定顏色字元串的圖片

     * 

     * @param message

     *            字元串

     * @param fontSize

     *            字體大小

     * @param color

     *            字體顏色

     * @return 圖片

     */

    public BufferedImage drawString(String message, int fontSize, Color color) {

        Graphics g = image.getGraphics();

        g.setColor(color);

        Font f = new Font(“宋體”, Font.BOLD, fontSize);

        g.setFont(f);

        int len = message.length();

        g.drawString(message, (width – fontSize * len) / 2,

                (height + (int) (fontSize / 1.5)) / 2);

        g.dispose();

        return image;

    }

    /**

     * 縮放圖片

     * 

     * @param scaleW

     *            水平縮放比例

     * @param scaleY

     *            垂直縮放比例

     * @return

     */

    public BufferedImage scale(double scaleW, double scaleH) {

        width = (int) (width * scaleW);

        height = (int) (height * scaleH);

        BufferedImage newImage = new BufferedImage(width, height,

                image.getType());

        Graphics g = newImage.getGraphics();

        g.drawImage(image, 0, 0, width, height, null);

        g.dispose();

        image = newImage;

        return image;

    }

    /**

     * 旋轉90度旋轉

     * 

     * @return 對應圖片

     */

    public BufferedImage rotate() {

        BufferedImage dest = new BufferedImage(height, width,

                BufferedImage.TYPE_INT_ARGB);

        for (int i = 0; i  width; i++)

            for (int j = 0; j  height; j++) {

                dest.setRGB(height – j – 1, i, image.getRGB(i, j));

            }

        image = dest;

        return image;

    }

    /**

     * 合併兩個圖像

     * 

     * @param anotherImage

     *            另一張圖片

     * @return 合併後的圖片,如果兩張圖片尺寸不一致,則返回null

     */

    public BufferedImage mergeImage(BufferedImage anotherImage) {

        int w = anotherImage.getWidth();

        int h = anotherImage.getHeight();

        if (w != width || h != height) {

            return null;

        }

        for (int i = 0; i  w; i++) {

            for (int j = 0; j  h; j++) {

                int rgb1 = image.getRGB(i, j);

                int rgb2 = anotherImage.getRGB(i, j);

                Color color1 = new Color(rgb1);

                Color color2 = new Color(rgb2);

                // 如果該位置兩張圖片均沒有字體經過,則跳過

                // 如果跳過,則最後將會是黑色背景

                if (color1.getRed() + color1.getGreen() + color1.getBlue()

                        + color2.getRed() + color2.getGreen()

                        + color2.getBlue() == 0) {

                    continue;

                }

                Color color = new Color(

                        (color1.getRed() + color2.getRed()) / 2,

                        (color1.getGreen() + color2.getGreen()) / 2,

                        (color1.getBlue() + color2.getBlue()) / 2);

                image.setRGB(i, j, color.getRGB());

            }

        }

        return image;

    }

    /**

     * 保存圖片int rgb1 = image.getRGB(i, j); int rgb2 = anotherImage.getRGB(i, j);

     * rgb2 = rgb1  rgb2; image.setRGB(height – i, j, rgb2);

     * 

     * @param filePath

     *            圖片路徑

     */

    public void save(String filePath) {

        try {

            ImageIO.write(image, “png”, new File(filePath));

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    /**

     * 得到對應的圖片

     * 

     * @return

     */

    public BufferedImage getImage() {

        return image;

    }

}

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如何生成顏色相間的條紋圖片

這是簡單的初步實現,你可以借鑒一下哈

package v01;

import javax.swing.*;

import java.awt.*;

public class ColorP extends JFrame {

public ColorP(){

this.add(new NewPanel());

}

public static void main(String[] args) {

ColorP frame = new ColorP();

frame.setTitle(“Content”);

frame.setSize(300, 150);

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

class NewPanel extends JPanel{//擴展JPanel

public NewPanel(){

}

protected void paintComponent(Graphics g){//運用畫筆Graphics繪製

super.paintComponent(g);//添加畫筆

int[] x = new int[4];

int[] y = new int[4];

for(int i = 0; i 10; i++){

x[0] = i * 40 ;y[0] = 0;

x[1] = x[0] + 40;y[1] = 0;

x[2] = 0;y[2] = (i+1) * 40;

x[3] = 0;y[3] = y[2] – 40 ;

if(i % 2 == 0)

g.setColor(Color.black);

else

g.setColor(Color.yellow);

g.fillPolygon(x, y, x.length);

}

}

}

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/285135.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-22 15:43
下一篇 2024-12-22 15:43

相關推薦

  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • Java Bean載入過程

    Java Bean載入過程涉及到類載入器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean載入的過程。 一、類載入器 類載入器是Java虛擬機…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • 用Python繪製酷炫圖片

    在本篇文章中,我們將展示如何使用Python繪製酷炫的圖片。 一、安裝Python繪圖庫 在使用Python繪製圖片之前,我們需要先安裝Python繪圖庫。Python有很多繪圖庫…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字元串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字元串中是否存在多個指定字元: 一、字元串遍歷 字元串是Java編程中非常重要的一種數據類型。要判斷字元串中是否存在多個指定字元…

    編程 2025-04-29
  • 使用Treeview顯示錶格

    在web開發中,顯示錶格數據是一項很常見的需求。當我們需要在頁面上顯示大量數據時,除了使用傳統的表格樣式外,還可以使用Treeview這種可摺疊的表格樣式,以便更好地展示數據。本文…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29

發表回復

登錄後才能評論