whycxzp
2022-05-30 76d39ee388b34c9b42a8dc1fbf88b506c9ce0054
word转pdf
1个文件已修改
5个文件已添加
482 ■■■■■ 已修改文件
pom.xml 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/Word2PdfController.java 64 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/util/LabelImage.java 327 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/util/TensorFlowUtil.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/util/Word2PdfJacobUtil.java 71 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/lib/jacob.jar 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml
@@ -181,6 +181,22 @@
            <scope>system</scope>
            <systemPath>${pom.basedir}/src/main/resources/lib/arcsoft-sdk-face-3.0.0.0_linux.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.tensorflow</groupId>
            <artifactId>tensorflow</artifactId>
            <version>1.15.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/net.sf.jacob-project/jacob -->
        <dependency>
            <groupId>net.sf.jacob-project</groupId>
            <artifactId>jacob</artifactId>
            <version>1.17-M2</version>
            <scope>system</scope>
            <systemPath>${pom.basedir}/src/main/resources/lib/jacob.jar</systemPath>
        </dependency>
    </dependencies>
    <build>
src/main/java/com/whyc/controller/Word2PdfController.java
New file
@@ -0,0 +1,64 @@
package com.whyc.controller;
import com.whyc.constant.YamlProperties;
import com.whyc.dto.Response;
import com.whyc.pojo.WeatherCity;
import com.whyc.util.Word2PdfJacobUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.*;
import java.io.File;
@RestController
@RequestMapping("word2Pdf")
@Api(tags = "word转化为pdf")
public class Word2PdfController {
    @PostMapping("transfer")
    @ApiOperation(value = "转化")
    public Response transfer(@RequestParam String url){
        String[] urlSplit = url.split("=");
        String fileUrl = urlSplit[1].replace("&fileName","");
        String fileName = urlSplit[2].split("\\.")[0];
        String filePath;
        //存储路径
        //项目jar同级目录下,图片能通过http方式访问到,很重要!
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        File jarFile = applicationHome.getDir();
        if(YamlProperties.runModel == 1) {
            //开发路径
            //baseDirPath = jarFile.getParentFile().toString()+File.separator;
            filePath = jarFile.getParentFile().toString()+ File.separator+"fg_photo"+File.separator+"zentao";
            File file = new File(filePath);
            if(!file.exists()){
                file.mkdirs();
            }
        }else {
            //打包路径
            //baseDirPath = jarFile.toString()+File.separator;
            filePath = jarFile.toString()+File.separator+"fg_photo"+File.separator+"zentao";
            File file = new File(fileName);
            if(!file.exists()){
                file.mkdirs();
            }
        }
        Word2PdfJacobUtil.word2PDF(fileUrl,filePath+File.separator+fileName+".pdf");
        return new Response().set(1,true,"转化成功");
    }
    public static void main(String[] args) {
        String a = "word.docx";
        String fileName = a.split("\\.")[0];
        System.out.println(fileName);
        String filePath ="F:\\zentao\\you";
        File file = new File(filePath);
        boolean exists = file.exists();
        if(!exists){
            boolean mkdir = file.mkdirs();
            System.out.println(mkdir);
        }
    }
}
src/main/java/com/whyc/util/LabelImage.java
New file
@@ -0,0 +1,327 @@
package com.whyc.util;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import org.tensorflow.*;
import org.tensorflow.types.UInt8;
/** Sample use of the TensorFlow Java API to label images using a pre-trained model. */
public class LabelImage {
    private static void printUsage(PrintStream s) {
        final String url =
                "https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip";
        s.println(
                "Java program that uses a pre-trained Inception model (http://arxiv.org/abs/1512.00567)");
        s.println("to label JPEG images.");
        s.println("TensorFlow version: " + TensorFlow.version());
        s.println();
        s.println("Usage: label_image <model dir> <image file>");
        s.println();
        s.println("Where:");
        s.println("<model dir> is a directory containing the unzipped contents of the inception model");
        s.println("            (from " + url + ")");
        s.println("<image file> is the path to a JPEG image file");
    }
    public static void main(String[] args) {
        test2(args);
    }
    private static void test2(String[] args){
        String exportDir = "F:\\tensorflow\\retrain\\saved_model\\";
        String imageFile = "F:\\tensorflow\\retrain\\test_images\\b1.jpg";
        String labelDir = "F:\\tensorflow\\retrain\\";
        SavedModelBundle model = SavedModelBundle.load(exportDir, "serve");
        List<String> labels =
                readAllLinesOrExit(Paths.get(labelDir, "output_labels.txt"));
        //readAllLinesOrExit(Paths.get(modelDir, "imagenet_comp_graph_label_strings.txt"));
        byte[] imageBytes = readAllBytesOrExit(Paths.get(imageFile));
        //对输入的图片进行处理
        Tensor<Float> image = constructAndExecuteGraphToNormalizeImage2(imageBytes);
        float[] labelProbabilities = executeInceptionGraph2(model, image);
        /*int bestLabelIdx = maxIndex(labelProbabilities);
        System.out.println(
                String.format("BEST MATCH: %s (%.2f%% likely)",
                        labels.get(bestLabelIdx),
                        labelProbabilities[bestLabelIdx] * 100f));*/
        for (int i = 0; i < labelProbabilities.length; i++) {
            System.out.println(
                    String.format("BEST MATCH: %s (%.2f%% likely)",
                            labels.get(i),
                            labelProbabilities[i] * 100f));
        }
    }
    /**
     * 原案例
     * @param args
     */
    private static void test(String[] args){
        if (args.length != 2) {
            printUsage(System.err);
            System.exit(1);
        }
        String modelDir = args[0];
        String imageFile = args[1];
        //byte[] graphDef = readAllBytesOrExit(Paths.get(modelDir, "tensorflow_inception_graph.pb"));
        //byte[] graphDef = readAllBytesOrExit(Paths.get(modelDir, "output_graph.pb"));
        //byte[] graphDef = SavedModelBundle.load("F:\\tensorflow\\retrain\\saved_model3", "serve").metaGraphDef();
        SavedModelBundle model = SavedModelBundle.load("F:\\tensorflow\\retrain\\saved_model3", "serve");
        List<String> labels =
                readAllLinesOrExit(Paths.get(modelDir, "output_labels.txt"));
        //readAllLinesOrExit(Paths.get(modelDir, "imagenet_comp_graph_label_strings.txt"));
        byte[] imageBytes = readAllBytesOrExit(Paths.get(imageFile));
        try (Tensor<Float> image = constructAndExecuteGraphToNormalizeImage(imageBytes)) {
        //try (Tensor<Float> image = constructAndExecuteGraphToNormalizeImage(model,imageBytes)) {
            float[] labelProbabilities = executeInceptionGraph(model.metaGraphDef(),image);
            int bestLabelIdx = maxIndex(labelProbabilities);
            System.out.println(
                    String.format("BEST MATCH: %s (%.2f%% likely)",
                            labels.get(bestLabelIdx),
                            labelProbabilities[bestLabelIdx] * 100f));
        }
    }
    private static Tensor<Float> constructAndExecuteGraphToNormalizeImage(byte[] imageBytes) {//把图片转换成inception需要模式
        try (Graph g = new Graph()) {//创建一个空的构造方法
            GraphBuilder b = new GraphBuilder(g);
            // Some constants specific to the pre-trained model at:
            // https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip
            //
            // - The model was trained with images scaled to 224x224 pixels.
            // - The colors, represented as R, G, B in 1-byte each were converted to
            //   float using (value - Mean)/Scale.
            final int H = 224;
            final int W = 224;
            final float mean = 117f;
            final float scale = 1f;
            // Since the graph is being constructed once per execution here, we can use a constant for the
            // input image. If the graph were to be re-used for multiple input images, a placeholder would
            // have been more appropriate.
            final Output<String> input = b.constant("DecodeJpeg/contents", imageBytes);//DecodeJpeg/contents:0
            final Output<Float> output =
                    b.div(
                            b.sub(
                                    b.resizeBilinear(
                                            b.expandDims(
                                                    b.cast(b.decodeJpeg(input, 3), Float.class),
                                                    b.constant("make_batch", 0)),
                                            b.constant("size", new int[] {H, W})),
                                    b.constant("mean", mean)),
                            b.constant("scale", scale));
            try (Session s = new Session(g)) {
                // Generally, there may be multiple output tensors, all of them must be closed to prevent resource leaks.
                return s.runner().fetch(output.op().name()).run().get(0).expect(Float.class);
            }
        }
    }
    private static Tensor<Float> constructAndExecuteGraphToNormalizeImage2(byte[] imageByte) {
    Graph g = new Graph();
    GraphBuilder b = new GraphBuilder(g);
    // Some constants specific to the pre-trained model at:
    // https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip
    //
    // - The model was trained with images scaled to 224x224 pixels.
    // - The colors, represented as R, G, B in 1-byte each were converted to
    //   float using (value - Mean)/Scale.
    //final int H = 224;
    final int H = 299;
    final int W = 299;
    //final int W = 224;
    //需要实际调试
    final float mean = 0.5f;
    //final float mean = 127.5f;
    //final float scale = 1f;
    //final float scale = 127.5f;
    final float scale = 1250f;
    // Since the graph is being constructed once per execution here, we can use a constant for the
    // input image. If the graph were to be re-used for multiple input images, a placeholder would
    // have been more appropriate.
    //final Output<String> input = b.constant("input",imageByte);
    final Output<String> input = b.constant("DecodeJpeg/contents",imageByte);
    final Output<Float> output =
            b.div(
                    b.sub(
                            b.resizeBilinear(
                                    b.expandDims(
                                            b.cast(b.decodeJpeg(input, 3), Float.class),
                                            b.constant("make_batch", 0)),
                                    b.constant("size", new int[] {H, W})),
                            b.constant("mean", mean)),
                    b.constant("scale", scale));
        // Generally, there may be multiple output tensors, all of them must be closed to prevent resource leaks.
        //return model.session().runner().fetch(output).run().get(0).expect(Float.class);
        Session s = new Session(g);
        return s.runner().fetch(output.op().name()).run().get(0).expect(Float.class);
    }
    private static float[] executeInceptionGraph(byte[] graphDef, Tensor<Float> image) {
        try (Graph g = new Graph()) {
            g.importGraphDef(graphDef);
            try (Session s = new Session(g);
                 // Generally, there may be multiple output tensors, all of them must be closed to prevent resource leaks.
                 Tensor<Float> result =
                         s.runner().feed("input", image).fetch("output").run().get(0).expect(Float.class)) {
                         //s.runner().feed("Placeholder:0", image).fetch("final_result:0").run().get(0).expect(Float.class)) {
                final long[] rshape = result.shape();
                if (result.numDimensions() != 2 || rshape[0] != 1) {
                    throw new RuntimeException(
                            String.format(
                                    "Expected model to produce a [1 N] shaped tensor where N is the number of labels, instead it produced one with shape %s",
                                    Arrays.toString(rshape)));
                }
                int nlabels = (int) rshape[1];
                return result.copyTo(new float[1][nlabels])[0];
            }
        }
    }
    private static float[] executeInceptionGraph2(SavedModelBundle model, Tensor<Float> image) {
            Session s = model.session();
            // Generally, there may be multiple output tensors, all of them must be closed to prevent resource leaks.
                 Tensor<Float> result =
                         //s.runner().feed("input", image).fetch("output").run().get(0).expect(Float.class)) {
                         s.runner().feed("Placeholder:0", image).fetch("final_result:0").run().get(0).expect(Float.class); {
                final long[] rshape = result.shape();
                if (result.numDimensions() != 2 || rshape[0] != 1) {
                    throw new RuntimeException(
                            String.format(
                                    "Expected model to produce a [1 N] shaped tensor where N is the number of labels, instead it produced one with shape %s",
                                    Arrays.toString(rshape)));
                }
                int nlabels = (int) rshape[1];
                return result.copyTo(new float[1][nlabels])[0];
        }
    }
    private static int maxIndex(float[] probabilities) {
        int best = 0;
        for (int i = 1; i < probabilities.length; ++i) {
            if (probabilities[i] > probabilities[best]) {
                best = i;
            }
        }
        return best;
    }
    private static byte[] readAllBytesOrExit(Path path) {
        try {
            return Files.readAllBytes(path);
        } catch (IOException e) {
            System.err.println("Failed to read [" + path + "]: " + e.getMessage());
            System.exit(1);
        }
        return null;
    }
    private static List<String> readAllLinesOrExit(Path path) {
        try {
            return Files.readAllLines(path, Charset.forName("UTF-8"));
        } catch (IOException e) {
            System.err.println("Failed to read [" + path + "]: " + e.getMessage());
            System.exit(0);
        }
        return null;
    }
    // In the fullness of time, equivalents of the methods of this class should be auto-generated from
    // the OpDefs linked into libtensorflow_jni.so. That would match what is done in other languages
    // like Python, C++ and Go.
    static class GraphBuilder {
        GraphBuilder(Graph g) {
            this.g = g;
        }
        Output<Float> div(Output<Float> x, Output<Float> y) {
            return binaryOp("Div", x, y);
        }
        Output<Float> holder(Output<Float> x, Output<Float> y) {
            return binaryOp("Placeholder:0", x, y);
        }
        <T> Output<T> sub(Output<T> x, Output<T> y) {
            return binaryOp("Sub", x, y);
        }
        <T> Output<Float> resizeBilinear(Output<T> images, Output<Integer> size) {
            return binaryOp3("ResizeBilinear", images, size);
        }
        <T> Output<T> expandDims(Output<T> input, Output<Integer> dim) {
            return binaryOp3("ExpandDims", input, dim);
        }
        <T, U> Output<U> cast(Output<T> value, Class<U> type) {
            DataType dtype = DataType.fromClass(type);
            return g.opBuilder("Cast", "Cast")
                    .addInput(value)
                    .setAttr("DstT", dtype)
                    .build()
                    .<U>output(0);
        }
        Output<UInt8> decodeJpeg(Output<String> contents, long channels) {
            return g.opBuilder("DecodeJpeg", "DecodeJpeg")
                    .addInput(contents)
                    .setAttr("channels", channels)
                    .build()
                    .<UInt8>output(0);
        }
        <T> Output<T> constant(String name, Object value, Class<T> type) {
            try (Tensor<T> t = Tensor.<T>create(value, type)) {
                return g.opBuilder("Const", name)
                        .setAttr("dtype", DataType.fromClass(type))
                        .setAttr("value", t)
                        .build()
                        .<T>output(0);
            }
        }
        Output<String> constant(String name, byte[] value) {
            return this.constant(name, value, String.class);
        }
        Output<Integer> constant(String name, int value) {
            return this.constant(name, value, Integer.class);
        }
        Output<Integer> constant(String name, int[] value) {
            return this.constant(name, value, Integer.class);
        }
        Output<Float> constant(String name, float value) {
            return this.constant(name, value, Float.class);
        }
        private <T> Output<T> binaryOp(String type, Output<T> in1, Output<T> in2) {
            return g.opBuilder(type, type).addInput(in1).addInput(in2).build().<T>output(0);
        }
        private <T, U, V> Output<T> binaryOp3(String type, Output<U> in1, Output<V> in2) {
            return g.opBuilder(type, type).addInput(in1).addInput(in2).build().<T>output(0);
        }
        private Graph g;
    }
}
src/main/java/com/whyc/util/TensorFlowUtil.java
New file
@@ -0,0 +1,4 @@
package com.whyc.util;
public class TensorFlowUtil {
}
src/main/java/com/whyc/util/Word2PdfJacobUtil.java
New file
@@ -0,0 +1,71 @@
package com.whyc.util;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class Word2PdfJacobUtil {
    /* 转PDF格式值 */
    private static final int wdFormatPDF = 17;
    /**
     * Word文档转换
     *
     * @param inputFile
     * @param pdfFile
     */
    public static boolean word2PDF(String inputFile, String pdfFile) {
        ComThread.InitMTA(true);
        long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent("Word.Application");// 创建一个word对象
            app.setProperty("Visible", new Variant(false)); // 不可见打开word
            app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
            Dispatch docs = app.getProperty("Documents").toDispatch();// 获取文挡属性
            //System.out.println("打开文档 >>> " + inputFile);
            // Object[]第三个参数是表示“是否只读方式打开”
            // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
            doc = Dispatch.call(docs, "Open", inputFile, false, true).toDispatch();
            //System.out.println("转换文档 [" + inputFile + "] >>> [" + pdfFile + "]");
            // 调用Document对象的SaveAs方法,将文档保存为pdf格式
            // word保存为pdf格式宏,值为17
            Dispatch.call(doc, "SaveAs", pdfFile, wdFormatPDF);// word保存为pdf格式宏,值为17
            long end = System.currentTimeMillis();
            System.out.println("用时:" + (end - start) + "ms.");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("========Error:文档转换失败:" + e.getMessage());
        } finally {
            Dispatch.call(doc, "Close", false);
            System.out.println("关闭文档");
            if (app != null)
                app.invoke("Quit", new Variant[] {});
            // 如果没有这句话,winword.exe进程将不会关闭
            ComThread.Release();
            ComThread.quitMainSTA();
        }
        return false;
    }
    public static void main(String[] arg){
        //String docPath = "C:\\Users\\29550\\Desktop\\研发人员考核方案.docx";
        String docPath = "http://118.89.139.230/zentao/data/upload/1/202205/1721233604702196";
        //String pdfPath = "C:\\Users\\29550\\Desktop\\研发人员考核方案2.pdf";
        String pdfPath = "F:\\pdf\\研发人员考核方案2.pdf";
        for (int i = 0; i < 20; i++) {
            boolean res = Word2PdfJacobUtil.word2PDF(docPath, pdfPath);
            System.out.println(res);
        }
    }
}
src/main/resources/lib/jacob.jar
Binary files differ