java.awt.Image到itextpdf.text.Image不起作用

时间:2016-11-09 17:48:38

标签: java maven itext barcode

您好我正在创建一个条形码,但是当我创建一个代码时,这会返回一个AwtImage,当我尝试解析到itextpdf.text.Image时,这会让我出错并且无法编译mi代码这是我的条形码生成器

import com.itextpdf.barcodes.Barcode128;
import com.itextpdf.text.Image;
import java.awt.Color;
import java.io.FileNotFoundException;

public Image getImage(){
        try{
            Barcode128 code128 = new Barcode128(null);
            code128.setCode("P662130002");
            code128.setBarHeight(80f);
            java.awt.Image awtImage = code128.createAwtImage(Color.WHITE, Color.BLACK);
            Image img = Image.getInstance(awtImage);
            return img;
        }catch(Exception e){
            return null;
        }
    }

这是我的maven依赖

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>kernel</artifactId>
        <version>7.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>io</artifactId>
        <version>7.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>forms</artifactId>
        <version>7.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdfa</artifactId>
        <version>7.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdftest</artifactId>
        <version>7.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.18</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.itextpdf/barcodes -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>barcodes</artifactId>
    <version>7.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.1.1</version>
</dependency>

1 个答案:

答案 0 :(得分:0)

我解决了这个问题你必须加一个额外的参数,这个代码是下一个

import com.itextpdf.barcodes.Barcode128;
import com.itextpdf.text.Image;
import java.awt.Color;
import java.io.FileNotFoundException;

public Image getImage(){
        try{
            Barcode128 code128 = new Barcode128(null);
            code128.setCode("P662130002");
            code128.setBarHeight(80f);
            java.awt.Image awtImage = code128.createAwtImage(Color.WHITE, Color.BLACK);
            Image img = Image.getInstance(awtImage);
            return img;
        }catch(Exception e){
            return null;
        }
    }