java调用BarTender
1.安装BarTender
此处不做赘述,网上安装教程很多。
2.api文件位置
API接口文档就在帮助文档的调用ActiveX执行自动化操作部分,其中有vb及C#如果调用BarTender的代码示例。
2.1 java调用com组件环境搭建
java调用com组件需要用到jacob依赖。在idea中创建demo项目,并创建libs目录,将jacob-xx.jar及jacob-xx-64.dll放到此目录下。
- 将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中示例。
2.3 demo部分
package com.example.demo.base;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Slf4j
@Component
@RequiredArgsConstructor
public class ComDemoRun {
private final ActiveXComponent dllApi;
@PostConstruct
public void init(){
dllApi.setProperty("Visible",new Variant(false));
log.info("1 >>>>>>>>>>>>>>>>>>>>>>>>>> {}",dllApi.getProgramId());
// dllApi.getProperty("Formats");
ActiveXComponent format = dllApi.getPropertyAsComponent("Formats");
ActiveXComponent doc = format.invokeGetComponent("Open",new Variant("C:\Users\Administrator\Desktop\demo1.btw"),new Variant(false),new Variant(""));
Variant count = format.getProperty("Count");
log.info("2 >>>>>>>>>>>>>>>>>>>>>>>>>> {}, count {}",format.getProgramId(),count.getInt());
ActiveXComponent fm = format.invokeGetComponent("GetFormat",new Variant(1));
doc.setProperty("UseDatabase",new Variant(true));
ActiveXComponent pros = doc.getPropertyAsComponent("NamedSubStrings");
Variant pc = pros.getProperty("Count");
for(int i = 1 ; i <= pc.getInt();i++){
ActiveXComponent pro = pros.invokeGetComponent("GetSubString",new Variant(i));
log.info("########################### {}",pro.getProperty("Name").getString());
if("Code".equals(pro.getProperty("Name").getString())){
pro.setProperty("Value","16637373");
}
}
ActiveXComponent data = doc.getPropertyAsComponent("Databases");
Dispatch dr = data.invokeGetComponent("GetDatabase",new Variant(1)).getObject();
Variant r = pros.invoke("GetAll",new Variant(","),new Variant(":"));
Variant name = Dispatch.get(dr,"Name");
Variant dt = Dispatch.get(dr,"Type");
log.info("{} json数据源", dt.getInt() == 17 ? "是": "不是");
if(dt.getInt() == 17){
ActiveXComponent json = ActiveXComponent.createNewInstance("Database.JSONDatabase");
json.setProperty("EmbeddedData",new Variant("{"tiaoxingma":"1663883"}"));
}
r = pros.invoke("GetAll",new Variant(","),new Variant(":"));
//分辨率必须介于 50 和 3000 之间
fm.invoke("ExportToFile",new Variant("D:\makekke.jpg"),new Variant("JPG"),new Variant(1),new Variant(2000),new Variant(2));
fm.invoke("Close",new Variant(2));
dllApi.invoke("Quit",2);
}
}