提交 cae3caac authored 作者: Whispa's avatar Whispa

commit

上级 ca3aa1e9
......@@ -54,6 +54,13 @@
<artifactId>unirest-java</artifactId>
<version>1.4.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-vision -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-vision</artifactId>
<version>1.100.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hashids/hashids -->
<dependency>
<groupId>org.hashids</groupId>
......
......@@ -2,7 +2,12 @@ package com.example.afrishop_v3.controllers;
import com.example.afrishop_v3.util.HttpClientUtil;
import com.example.afrishop_v3.util.IdUtil;
import com.google.api.gax.paging.Page;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.common.collect.Lists;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseAuth;
......@@ -22,7 +27,7 @@ import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
class Controller {
abstract class Controller {
String uid() {
return IdUtil.createIdbyUUID();
......@@ -32,6 +37,21 @@ class Controller {
static int id = 0;
NumberFormat formatter = new DecimalFormat("#0.00");
void authExplicit() throws IOException {
// You can specify a credential file by providing a path to GoogleCredentials.
// Otherwise credentials are read from the GOOGLE_APPLICATION_CREDENTIALS environment variable.
InputStream in = new ClassPathResource("afrishop-6e142-a74ff3091e0c.json").getInputStream();
GoogleCredentials credentials = GoogleCredentials.fromStream(in)
.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
System.out.println("Buckets:");
Page<Bucket> buckets = storage.list();
for (Bucket bucket : buckets.iterateAll()) {
System.out.println(bucket.toString());
}
}
private FirebaseApp getApp() {
FirebaseOptions options;
try {
......
package com.example.afrishop_v3.controllers;
import com.example.afrishop_v3.base.Result;
import com.example.afrishop_v3.enums.ResultCodeEnum;
import com.google.api.client.util.IOUtils;
import com.google.cloud.vision.v1.*;
import com.google.cloud.vision.v1.Feature.Type;
import com.google.protobuf.ByteString;
import com.google.protobuf.Descriptors;
import com.google.protobuf.GeneratedMessageV3;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@RestController
@RequestMapping("search/image")
public class ImageSearchController extends Controller{
// Imports the Google Cloud client library
@GetMapping
public Result main() throws Exception {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
Result errorDecoding = new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(), "Error decoding");
authExplicit();
try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) {
// The path to the image file to annotate
String fileName = "./resources/wakeupcat.jpg";
// Reads the image file into memory
//Path path = Paths.get(fileName);
InputStream in = new ClassPathResource("Blue_Tshirt.jpg").getInputStream();
byte[] data = IOUtils.serialize(in);
//byte[] data = Files.readAllBytes(path);
ByteString imgBytes = ByteString.copyFrom(data);
// Builds the image annotation request
List<AnnotateImageRequest> requests = new ArrayList<>();
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Type.OBJECT_LOCALIZATION).build();
AnnotateImageRequest request =
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);
// Performs label detection on the image file
BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.format("Error: %s%n", res.getError().getMessage());
return errorDecoding;
}
List<Map<Descriptors.FieldDescriptor, Object>> collect = res.getLocalizedObjectAnnotationsList().stream().map(GeneratedMessageV3::getAllFields).collect(Collectors.toList());
// for (LocalizedObjectAnnotation annotation : res.getLocalizedObjectAnnotationsList()) {
// annotation
// .getAllFields()
// .forEach((k, v) -> System.out.format("%s : %s%n", k., v.toString()));
// }
return new Result<>(collect);
}
}
return errorDecoding;
}
}
......@@ -58,7 +58,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
http.cors().and().csrf().disable()
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests().antMatchers("/api/auth/**", "/itemStation/**", "/startPage/**", "/goodsType/**", "/home/**", "/spider/**", "/store/**", "/shopify/**", "/community/**","/version/**").permitAll()
.authorizeRequests().antMatchers("/api/auth/**","/search/image/**", "/itemStation/**", "/startPage/**", "/goodsType/**", "/home/**", "/spider/**", "/store/**", "/shopify/**", "/community/**","/version/**").permitAll()
.antMatchers("/api/test/**").permitAll()
.anyRequest().authenticated();
......
{
"type": "service_account",
"project_id": "afrishop-6e142",
"private_key_id": "a74ff3091e0cea23f89120f483ae313694a2c69e",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCou2lJZ78yw0LS\njJvu3Qu1frgP5WJ2w+I6ornJ8Dasf5NeNPoeOUAmT3eZwInuGAwknd9hFwAci9FP\nM6WO4XX/6ckOyzTb0yDbUcpimpiJy0DP8HRBsVxVDonvUGqdRG2c/VYapxoW63y7\nNs/utM31W+MIUgFw6Tw5RGqFh4x4FBk5nEWbUYNGPyqFNzlPjhkYOpwRi3r6h46p\nBPj77V+JyHjZFjM0X24bNxUP6FHDP4Ei4YODNIlPePV3DthYSa3z5RX9BzQiZ6tk\n1CmuzPFR1UUx0Kv392UpmuZ8gFiUTzI7MaJkB9gyxt/3bnil6hjU5RxcVTVy6i/t\nDnp07X/ZAgMBAAECggEAC+d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EcBMw9k+bAoGBAISbbAAsfcS04JjorSrh\nUSMMjcSSOvo2sKzZNkLNUNcBhZhsjFyE1OcX0Sne/nlRwXo6JiusGSC6nKuWNYSf\nt1TTMlD0upjfR4xBDn8xsoVijt251tFRqegPBSaPD4wfv/rGLBoOfEmrZQstRaaq\npzr0ho3T9W8GosN24WAHhLIX\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-ypj91@afrishop-6e142.iam.gserviceaccount.com",
"client_id": "110654393006448219899",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-ypj91%40afrishop-6e142.iam.gserviceaccount.com"
}
server.servlet.context-path=/zion
spring.jpa.hibernate.ddl-auto=update
server.port = 7000
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/chinafrica?useUnicode=true&connectionCollation=utf8mb4_general_ci&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:165.22.82.105}:3306/chinafrica?useUnicode=true&connectionCollation=utf8mb4_general_ci&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=clement123
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论