「java文字转换为图片」java文件转图片

博主:adminadmin 2022-11-30 02:06:07 67

本篇文章给大家谈谈java文字转换为图片,以及java文件转图片对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

求助,帮我用JAVA语言写一个字节转换成图片的方法,返回图片类型

我直接把做验证码的代码发出来了。如果你不是要做验证码。那你自己看下就知道你问题的答案了。

//清空浏览器缓存。确保验证图片更换

response.setHeader("Cache-Control","no-cache");

response.setHeader("Pragma", "no-cache");

response.setDateHeader("Expires", -1);

//以图片形式打开数据

response.setHeader("Content-Type","image/jpeg");

//response.setContentType("image/jpeg");

//在内存中创建一副图片

BufferedImage image=new BufferedImage(80,30,BufferedImage.TYPE_INT_RGB);

//在图片上写数据

Graphics g=image.getGraphics();

g.setColor(Color.black);

//画个宽为80.、高30的实心矩形刚好将图片遮盖

g.fillRect(0, 0, 80, 30);

//设置图片上字体的颜色及格式

g.setColor(Color.red);

g.setFont(new Font("宋体",Font.BOLD,20));

//将字体写入图片

String num=makeNum();

//将字串保存到session中

request.getSession().setAttribute("checkcode", num);

//写字符串。坐标是从基线为准。

g.drawString(num, 0, 20);

//将图片输出给浏览器。参数分别为要写入的 RenderedImage。 包含格式非正式名称的 String。 将在其中写入数据的 OutputStream。

ImageIO.write(image, "jpg",response.getOutputStream());

}

public String makeNum(){

Random r=new Random();

String num=r.nextInt(10000000)+"";

StringBuffer stringBuffer=new StringBuffer();

for (int i = 0; i 7-num.length(); i++) {

stringBuffer.append("0");

}

num=stringBuffer.toString()+num;

return num;

}

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、读取txt文本的每一行

2、创建BufferedImage图片,然后在图片上画读取到的文本

下面给出示例程序:

测试类 TextToImageExample.java

import java.io.File;

import java.util.Scanner;

/**

 * 文本转图片测试类

 * @author YY2924 2014/11/18

 * @version 1.0

 */

public class TextToImageExample {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        System.out.print("输入TXT文本名称 (例如: D:/java.txt ):");

        String textFileName = in.nextLine();

        System.out.print("输入保存的图片名称 (例如: D:/java.jpg):");

        String imageFileName = in.nextLine();

        

        TextToImage convert = new TextToImage(new File(textFileName), new File(imageFileName));

        boolean success = convert.convert();

        System.out.println("文本转图片:" + (success ? "成功" : "失败"));

    }

}

文本转图片类 TextToImage.java

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

import com.sun.image.codec.jpeg.JPEGCodec;

/**

 * 文本转图片类

 * @author YY2924 2014/11/18

 * @version 1.0

 */

public class TextToImage {

    /** 文本文件  */

    private File textFile;

    /** 图片文件 */

    private File imageFile;

    

    /** 图片 */

    private BufferedImage image;

    /** 图片宽度  */

    private final int IMAGE_WIDTH = 400;

    /** 图片高度 */

    private final int IMAGE_HEIGHT = 600;

    /** 图片类型  */

    private final int IMAGE_TYPE = BufferedImage.TYPE_INT_RGB;

    

    /**

     * 构造函数

     * @param textFile 文本文件

     * @param imageFile 图片文件

     */

    public TextToImage(File textFile,File imageFile){

        this.textFile = textFile;

        this.imageFile = imageFile;

        this.image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_TYPE);

    }

    

    /**

     * 将文本文件里文字,写入到图片中保存

     * @return boolean  true,写入成功;false,写入失败

     */

    public boolean convert() {

        

        //读取文本文件

        BufferedReader reader = null;

        try {

            reader = new BufferedReader(new FileReader(textFile));

        } catch (FileNotFoundException e) {

            e.printStackTrace();

            return false;

        }

        

        //获取图像上下文

        Graphics g = createGraphics(image);

        String line;

        //图片中文本行高

        final int Y_LINEHEIGHT = 15;

        int lineNum = 1;

        try {

            while((line = reader.readLine()) != null){

                g.drawString(line, 0, lineNum * Y_LINEHEIGHT);

                lineNum++;

            }

            g.dispose();

            

            //保存为jpg图片

            FileOutputStream fos = new FileOutputStream(imageFile);

            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);

            encoder.encode(image);

            fos.close();

        } catch (IOException e) {

            e.printStackTrace();

            return false;

        }

        return true;

    }

    

    /**

     * 获取到图像上下文

     * @param image 图片

     * @return Graphics

     */

    private Graphics createGraphics(BufferedImage image){

        Graphics g = image.createGraphics();

        g.setColor(Color.WHITE); //设置背景色

        g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);//绘制背景

        g.setColor(Color.BLACK); //设置前景色

        g.setFont(new Font("微软雅黑", Font.PLAIN, 12)); //设置字体

        return g;

    }

    

}

特别注意:程序中使用到了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可以将字符串转化为图片吗

可以! 但不能把字符串转化为图片格式哦 只能把字符串写在图片上 形成图片

//创建一张图片

BufferedImage image = new BufferedImage(150,30,BufferedImage.TYPE_INT_RGB);

//得到图片

Graphics2D g = (Graphics2D)image.getGraphics();

g.setColor(getRandColor(160, 200));

g.fillRect(0, 0, 200, 30);

//向图片上写写数据

g.setColor(Color.blue);//设置图片颜色

g.setFont(new Font("隶书",Font.ITALIC,20));

//把想要写的字符串画在图片上

String code = "^^^^^^^^^^^^^^^^"

g.drawString(code, 10, 25);//画图片

request.getSession().setAttribute("code", code);//把图片存在session中

ImageIO.write(image,"jpg", response.getOutputStream());//输出图片

}

java 16进制字符串转图片

String src=...; //从数据库取得的字符串

String output=...; //定义一个输出流用来保存图片

try{

FileOutputStream out = new FileOutputStream(new File(output));

byte[] bytes = src.getBytes();

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

out.write(charToInt(bytes[i])*16+charToInt(bytes[i+1]));

}

out.close();

}catch(Exception e){

e.printStackTrace();

}

java如何将连续的字节数据转成图片数据?

java是可以将图片字节转成图片的,但是你需要知道接受的数据对应一个图片有多长,可以在每个图片数据包前面加上此次图片的长度,按长度截取字节,转成图片,或者直接将字节转成视频,毕竟你是连续的图像

关于java文字转换为图片和java文件转图片的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-30,除非注明,否则均为首码项目网原创文章,转载请注明出处。