下载APP

java调用BarTender

java调用BarTender

1.安装BarTender

此处不做赘述,网上安装教程很多。

2.api文件位置

API接口文档就在帮助文档的调用ActiveX执行自动化操作部分,其中有vb及C#如果调用BarTender的代码示例。 1676716247946.png

2.1 java调用com组件环境搭建

java调用com组件需要用到jacob依赖。在idea中创建demo项目,并创建libs目录,将jacob-xx.jar及jacob-xx-64.dll放到此目录下。

1676716657433.png

  • 将jacob-x.jar安装到本地maven仓库
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>build-info</id>
                    <goals>
                        <goal>build-info</goal>
                    </goals>
                </execution>
                <execution>
                    <id>repackage</id>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>install-jacob</id>
                    <phase>validate</phase>
                    <configuration>
                        <file>${basedir}/libs/jacob.jar</file>
                        <groupId>net.sf.jacob-project</groupId>
                        <artifactId>jacob</artifactId>
                        <version>1.20</version>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                    </configuration>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-libs</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/libs</outputDirectory>
                        <resources>
                            <resource>
                                <directory>libs</directory>
                                <excludes>
                                    <exclude>**/*.TXT</exclude>
                                    <exclude>**/docs/**</exclude>
                                </excludes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

运行mvn install

  • 添加依赖
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>net.sf.jacob-project</groupId>
        <artifactId>jacob</artifactId>
        <version>1.20</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
  • 配置
package com.example.demo.config;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.LibraryLoader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.*;

@Slf4j
@Configuration
public class DemoJacobConfig {

    private static final String COM_INTERFACE_NAME = "BarTender.Application";

    private static final String JACOB_LIB_PATH = System.getProperty("user.dir") + "\libs\dll";
    private static final String LIB_FILE = System.getProperty("os.arch")
            .equals("amd64") ? "\jacob-1.20-x64.dll" : "\jacob-1.20-x86.dll";

    private File temporaryDll;

    static {
//        log.info("JACOB lib path: {}", JACOB_LIB_PATH);
//        log.info("JACOB file lib path: {}", JACOB_LIB_PATH + LIB_FILE);
        System.setProperty("java.library.path", JACOB_LIB_PATH);
        System.setProperty("com.jacob.debug", "true");
    }

    @PostConstruct
    public void init() throws IOException {
        InputStream inputStream = new FileInputStream(JACOB_LIB_PATH + LIB_FILE);

        temporaryDll = File.createTempFile("jacob", ".dll");
        FileOutputStream outputStream = new FileOutputStream(temporaryDll);
        byte[] array = new byte[8192];
        for (int i = inputStream.read(array); i != -1; i = inputStream.read(array)) {
            outputStream.write(array, 0, i);
        }
        outputStream.close();

        System.setProperty(LibraryLoader.JACOB_DLL_PATH, temporaryDll.getAbsolutePath());
        LibraryLoader.loadJacobLibrary();
        log.info("JACOB library is loaded and ready to use");
    }

    @Bean
    public ActiveXComponent dllAPI() {
        // ProgramId
        ActiveXComponent activeXComponent = new ActiveXComponent(COM_INTERFACE_NAME);
        log.info("API COM interface {} wrapped into ActiveXComponent is created and ready to use", COM_INTERFACE_NAME);
        return activeXComponent;
    }

    @PreDestroy
    public void clean() {
        temporaryDll.deleteOnExit();
        log.info("Temporary DLL API library is cleaned on exit");
    }
}

其中ProgramId可以参考api中示例。

1676717286010.png

在线举报