-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from lunasaw/3.2.0-SNAPSHOT
3.2.0 snapshot
- Loading branch information
Showing
65 changed files
with
594 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
ali-spring-boot-starter/src/main/java/com/luna/ali/oss/api/AliOssWebApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
api-spring-boot-starter/src/main/java/com/luna/api/config/XunfeiAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.luna.api.config; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.ComponentScans; | ||
|
||
import com.luna.api.xfyun.config.XunfeiProperties; | ||
|
||
/** | ||
* @author luna | ||
* @date 2024/6/20 | ||
*/ | ||
@AutoConfiguration | ||
@EnableConfigurationProperties({XunfeiAutoConfiguration.class}) | ||
@ComponentScans(value = {@ComponentScan(value = "com.luna.api.xfyun.*")}) | ||
@ConditionalOnClass(value = DataSourceAutoConfiguration.class) | ||
public class XunfeiAutoConfiguration { | ||
|
||
private XunfeiProperties xunfeiProperties; | ||
|
||
public XunfeiProperties getXunfeiProperties() { | ||
return xunfeiProperties; | ||
} | ||
|
||
@Autowired | ||
public void setXunfeiProperties(XunfeiProperties xunfeiProperties) { | ||
this.xunfeiProperties = xunfeiProperties; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
api-spring-boot-starter/src/main/java/com/luna/api/email/warpper/MailWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
api-spring-boot-starter/src/main/java/com/luna/api/smms/api/ImageApiFromString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
api-spring-boot-starter/src/main/java/com/luna/api/smms/api/UserApiFromFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
api-spring-boot-starter/src/main/java/com/luna/api/xfyun/config/XunfeiProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.luna.api.xfyun.config; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* @Package: com.luna.api.smMs | ||
* @ClassName: SmMsConfigValue | ||
* @Author: luna | ||
* @CreateTime: 2020/7/27 10:12 | ||
* @Description: | ||
*/ | ||
@Component | ||
@ConfigurationProperties(prefix = "spring.xunfei") | ||
@Data | ||
public class XunfeiProperties { | ||
|
||
private Boolean enable; | ||
|
||
private String apikey; | ||
private String appId; | ||
private String apiSecret; | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
api-spring-boot-starter/src/main/java/com/luna/api/xfyun/constant/XfConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.luna.api.xfyun.constant; | ||
|
||
/** | ||
* @author luna | ||
* @date 2024/6/20 | ||
*/ | ||
public interface XfConstant { | ||
|
||
|
||
String HOST = "https://api.xf-yun.com"; | ||
String PATH = "/v1/private/sf8e6aca1"; | ||
String REQUEST_URL = HOST + PATH; | ||
|
||
// 控制台获取以下信息 | ||
String APPID = "5eae601b"; | ||
String API_SECRET = "f8457806d8ae791dcfd250c1b4ca2ba8"; | ||
String API_KEY = "d0b88051dff7906c15ab700316a2a412"; | ||
|
||
} |
162 changes: 162 additions & 0 deletions
162
...g-boot-starter/src/main/java/com/luna/api/xfyun/ocr/UniversalCharacterRecognitionApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package com.luna.api.xfyun.ocr; | ||
|
||
import java.net.URL; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.text.SimpleDateFormat; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.crypto.Mac; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.hc.core5.http.ClassicHttpResponse; | ||
|
||
import com.alibaba.fastjson2.JSON; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.Lists; | ||
import com.luna.api.xfyun.constant.XfConstant; | ||
import com.luna.api.xfyun.ocr.dto.OcrRequest; | ||
import com.luna.api.xfyun.ocr.dto.OcrResponse; | ||
import com.luna.api.xfyun.ocr.dto.OcrTextDTO; | ||
import com.luna.common.check.Assert; | ||
import com.luna.common.constant.StrPoolConstant; | ||
import com.luna.common.encrypt.Base64Util; | ||
import com.luna.common.file.FileTools; | ||
import com.luna.common.net.HttpUtils; | ||
import com.luna.common.net.HttpUtilsConstant; | ||
|
||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* @author luna | ||
* @date 2024/6/20 | ||
*/ | ||
@Slf4j | ||
public class UniversalCharacterRecognitionApi { | ||
|
||
private static final String IMAGE_PATH = "/Users/weidian/Downloads/universal_character_recognition_java_demo/example/1.jpg"; | ||
|
||
@SneakyThrows | ||
public static void main(String[] args) { | ||
String s = getOcrText(XfConstant.REQUEST_URL, XfConstant.API_SECRET, XfConstant.API_KEY, IMAGE_PATH); | ||
System.out.println(s); | ||
OcrTextDTO parse = parse(s); | ||
System.out.println(JSON.toJSONString(getContent(parse))); | ||
} | ||
|
||
public static List<String> getContent(String filePath) { | ||
return getContent(XfConstant.API_SECRET, XfConstant.API_KEY, filePath); | ||
} | ||
|
||
public static List<String> getContent(String apiSecret, String apiKey, String filePath) { | ||
Assert.notNull(filePath, "文件路径不能为空"); | ||
String ocrText = getOcrText(XfConstant.API_SECRET, XfConstant.API_KEY, filePath); | ||
if (StringUtils.isBlank(ocrText)) { | ||
return Collections.emptyList(); | ||
} | ||
return getContent(parse(ocrText)); | ||
} | ||
|
||
public static List<String> getContent(OcrTextDTO ocrTextDTO) { | ||
if (ocrTextDTO == null) { | ||
return new ArrayList<>(); | ||
} | ||
List<OcrTextDTO.PagesItem> pages = ocrTextDTO.getPages(); | ||
if (CollectionUtils.isEmpty(pages)) { | ||
return new ArrayList<>(); | ||
} | ||
|
||
List<List<OcrTextDTO.LinesItem>> lines = pages.stream().map(OcrTextDTO.PagesItem::getLines).collect(Collectors.toList()); | ||
if (CollectionUtils.isEmpty(lines)) { | ||
return new ArrayList<>(); | ||
} | ||
|
||
ArrayList<String> words = Lists.newArrayList(); | ||
for (List<OcrTextDTO.LinesItem> line : lines) { | ||
if (CollectionUtils.isEmpty(line)) { | ||
continue; | ||
} | ||
List<String> strings = line.stream().map(OcrTextDTO.LinesItem::getWords) | ||
.filter(CollectionUtils::isNotEmpty) | ||
.map(e -> e.stream().map(OcrTextDTO.WordsItem::getContent) | ||
.filter(StringUtils::isNotBlank) | ||
.collect(Collectors.joining(StrPoolConstant.UNDERLINE))) | ||
.collect(Collectors.toList()); | ||
if (CollectionUtils.isEmpty(strings)) { | ||
continue; | ||
} | ||
words.addAll(strings); | ||
} | ||
return words; | ||
} | ||
|
||
public static OcrTextDTO parse(String data) { | ||
OcrResponse ocrResponse = JSON.parseObject(data, OcrResponse.class); | ||
if (ocrResponse == null) { | ||
return null; | ||
} | ||
OcrResponse.Payload payload = ocrResponse.getPayload(); | ||
if (payload == null) { | ||
return null; | ||
} | ||
String text = Optional.ofNullable(payload.getResult()).map(OcrResponse.Result::getText).orElse(StringUtils.EMPTY); | ||
String result = new String(Base64Util.decodeBase64(text), StandardCharsets.UTF_8); | ||
if (StringUtils.isEmpty(result)) { | ||
return null; | ||
} | ||
return JSON.parseObject(result, OcrTextDTO.class); | ||
} | ||
|
||
private static String getOcrText(String apiSecret, String apiKey, String filePath) { | ||
return getOcrText(XfConstant.REQUEST_URL, apiSecret, apiKey, filePath); | ||
} | ||
|
||
private static String getOcrText(String requestUrl, String apiSecret, String apiKey, String filePath) { | ||
OcrRequest ocrRequest = new OcrRequest(); | ||
ocrRequest.setHeader(new OcrRequest.Header(XfConstant.APPID, 3)); | ||
ocrRequest.setParameter(new OcrRequest.Parameter(OcrRequest.Sf8e6aca1.getInstance())); | ||
ocrRequest.setPayload( | ||
new OcrRequest.Payload(OcrRequest.Sf8e6aca1DataOne.getInstance(Base64.getEncoder().encodeToString(FileTools.read(filePath))))); | ||
Map<String, String> param = getUrlAuthPath(requestUrl, apiSecret, apiKey); | ||
|
||
ClassicHttpResponse classicHttpResponse = | ||
HttpUtils.doPost(XfConstant.HOST, XfConstant.PATH, ImmutableMap.of("Content-Type", HttpUtilsConstant.JSON), param, | ||
JSON.toJSONString(ocrRequest)); | ||
String s = HttpUtils.checkResponseAndGetResult(classicHttpResponse, false); | ||
System.out.println(s); | ||
return s; | ||
} | ||
|
||
@SneakyThrows | ||
private static Map<String, String> getUrlAuthPath(String requestUrl, String apiSecret, String apiKey) { | ||
Assert.notNull(apiSecret, "apiSecret不能为空"); | ||
Assert.notNull(apiKey, "apiKey不能为空"); | ||
|
||
// 替换调schema前缀 ,原因是URL库不支持解析包含ws,wss schema的url | ||
String httpRequestUrl = requestUrl.replace("ws://", "http://").replace("wss://", "https://"); | ||
URL url = new URL(httpRequestUrl); | ||
// 获取当前日期并格式化 | ||
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); | ||
format.setTimeZone(TimeZone.getTimeZone("GMT")); | ||
String date = format.format(new Date()); | ||
String host = url.getHost(); | ||
String builder = "host: " + host + "\n" + | ||
"date: " + date + "\n" + | ||
"POST " + url.getPath() + " HTTP/1.1"; | ||
Charset charset = StandardCharsets.UTF_8; | ||
Mac mac = Mac.getInstance("hmacsha256"); | ||
SecretKeySpec spec = new SecretKeySpec(apiSecret.getBytes(charset), "hmacsha256"); | ||
mac.init(spec); | ||
byte[] hexDigits = mac.doFinal(builder.getBytes(charset)); | ||
String sha = Base64.getEncoder().encodeToString(hexDigits); | ||
String authorization = | ||
String.format("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", apiKey, "hmac-sha256", "host date request-line", sha); | ||
String authBase = Base64.getEncoder().encodeToString(authorization.getBytes(charset)); | ||
|
||
return ImmutableMap.of("authorization", authBase, "host", host, "date", date); | ||
} | ||
} |
Oops, something went wrong.