Topik ini menjelaskan cara menggunakan Image Search SDK untuk Java serta menyediakan kode contoh.
Metode
Metode | Deskripsi |
Tambah | Menambahkan gambar. |
SearchImageByPic | Mencari gambar serupa berdasarkan gambar. |
SearchImageByName | Mencari gambar serupa berdasarkan nama gambar yang ada di galeri gambar. |
Hapus | Menghapus satu atau lebih gambar. |
UpdateImage | Memperbarui gambar. |
Detail | Meminta informasi instance. |
DumpMeta | Membuat tugas untuk mengekspor metadata. |
DumpMetaList | Meminta daftar tugas yang digunakan untuk mengekspor metadata. |
BatchTask | Membuat tugas batch. |
BatchTaskList | Meminta daftar tugas yang digunakan untuk melakukan operasi batch. |
CompareSimilarByImage | Membandingkan kesamaan antara dua gambar. |
Persiapan
Sebelum menginstal dan menggunakan Alibaba Cloud SDK, pastikan Anda telah membuat akun Alibaba Cloud dan mendapatkan pasangan AccessKey dari akun tersebut. Untuk informasi lebih lanjut, lihat Dapatkan Pasangan AccessKey.
Tambahkan Image Search SDK untuk Java ke proyek Anda.
Impor Image Search SDK untuk Java sebagai dependensi Maven, lalu tambahkan SDK ke proyek Anda.
<dependency> <groupId>com.aliyun</groupId> <artifactId>imagesearch20201214</artifactId> <version>4.2.2</version> </dependency>
Tambah
Kode contoh
import com.aliyun.imagesearch20201214.Client; import com.aliyun.imagesearch20201214.models.AddImageAdvanceRequest; import com.aliyun.imagesearch20201214.models.AddImageResponse; import com.aliyun.teaopenapi.models.Config; import com.aliyun.tea.TeaException; import com.aliyun.teautil.models.RuntimeOptions; import java.io.FileInputStream; import java.net.URL; import java.io.InputStream; public class Add { public static void main(String[] args) throws Exception { Config authConfig = new Config(); // For information about how to create an AccessKey pair, see "Create an AccessKey pair" at https://www.alibabacloud.com/help/en/resource-access-management/latest/accesskey-pairs-create-an-accesskey-pair-for-a-ram-user. // The AccessKey pair of an Alibaba Cloud account has the permissions to call all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. // We recommend that you do not hard code your AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account is compromised. // In this example, the AccessKey ID and AccessKey secret are saved as environment variables. You can also save the AccessKey pair in the configuration file based on your business requirements. authConfig.accessKeyId = System.getenv("CC_AK_ENV"); authConfig.accessKeySecret = System.getenv("CC_SK_ENV"); authConfig.type = "access_key"; authConfig.regionId = "ap-southeast-1"; // Replace the value with the ID of the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the regionId parameter to ap-southeast-1. authConfig.endpoint = "imagesearch.ap-southeast-1.aliyuncs.com"; // Replace the value with the endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch.ap-southeast-1.aliyuncs.com. // The following sample code provides an example on how to use a virtual private cloud (VPC) endpoint to call the Image Search API. // Note: You can use a VPC endpoint to call the Image Search API only from Elastic Compute Service (ECS) instances or resources that reside in the same region. For example, if your Image Search instance resides in Singapore, you can use a VPC endpoint to call the Image Search API only from the ECS instances or resources that reside in Singapore. Otherwise, the API operation fails to be called. In case of a failed API call, check whether your ECS instances or resources are in the same region as your Image Search instance before you call the Image Search API. // authConfig.endpointType = "internal"; // If you want to use a VPC endpoint to call the Image Search API, you must specify the endpointType parameter and set the value to internal. // authConfig.endpoint = "imagesearch-vpc.ap-southeast-1.aliyuncs.com"; // The VPC endpoint that is used to call the Image Search API. Replace the value with the VPC endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch-vpc.ap-southeast-1.aliyuncs.com. Client client = new Client(authConfig); AddImageAdvanceRequest request = new AddImageAdvanceRequest(); // Required. The name of the Image Search instance. Enter the instance name instead of the instance ID. After an Image Search instance is purchased, you can view the instance name on the instance list page of the Image Search console. request.instanceName = "XXXXXXXXXXX"; // Required. The ID of the product. The ID cannot exceed 256 characters in length. // A product ID can correspond to multiple image names. request.productId = "test"; // Required. The name of the image. The name can be up to 256 characters in length. // 1. An image is uniquely identified by the values of the productId and picName parameters. // 2. If you add an image whose product ID (productId) and image name (picName) are the same as those of an existing image, the newly added image overwrites the existing image. request.picName = "1000"; // Optional. The ID of the product category. // 1. Product image search: If a category is specified, the specified category prevails. If no category is specified, the system predicts and selects a category. The category selected by the system is included in the response. // 2. Generic image search: The category ID is set to 88888888 regardless of whether a category is specified. request.categoryId = 3; // Optional. The user-defined content. The value can be up to 4,096 characters in length. // If you specify this parameter, the response includes this parameter and its value. You can add text such as an image description. request.customContent = "this is a simple test"; // Optional. The attribute of the INT type. The attribute can be used to filter images during image searches. If you specify this parameter, the response includes this parameter and its value. // For example, you can set different attributes for images from different sites or different users. This way, users can filter images by attribute and obtain more accurate search results. request.intAttr = 56; // Optional. The attribute of the STRING type. The value can be up to 128 characters in length. The attribute can be used to filter images during image searches. If you specify this parameter, the response includes this parameter and its value. request.strAttr = "test"; // Optional. Specifies whether to identify the subject in the image. Default value: true. // 1. If you set this parameter to true, the system identifies the subject in the image and searches for images based on the identified subject. The response includes the identification result. // 2. If you set this parameter to false, the system searches for images based on the entire image without subject identification. request.crop = true; // Optional. The subject area of the image, in the format of x1,x2,y1,y2. Specifically, x1 and y1 specify the upper-left point, and x2 and y2 specify the lower-right point. The specified area cannot cross the boundary of the image. // If you specify the region parameter, the search is conducted based on the value of the region parameter regardless of the value of the crop parameter. request.region = "167,467,221,407"; RuntimeOptions runtimeOptions = new RuntimeOptions(); String picName = "D:/123.jpg"; try { // The image. The image cannot exceed 4 MB in size. The transmission timeout period cannot exceed 5 seconds. Only the following image formats are supported: PNG, JPG, JPEG, BMP, GIF, WebP, TIFF, and PPM. // For product and generic image searches, the length and the width of an image must range from 100 pixels to 4,096 pixels. // The image cannot contain rotation properties. // The following sample code provides an example on how to specify an image URL: // String url = "https://www.example.com/123.jpg"; // request.picContentObject = new URL(url).openStream(); // The following sample code provides an example on how to upload an image file: InputStream inputStream = new FileInputStream(picName); request.picContentObject = inputStream; AddImageResponse response = client.addImageAdvance(request,runtimeOptions); System.out.println("success: " + response.getBody().success + ". message: " + response.getBody().message + ". categoryId: " + response.getBody().picInfo.categoryId + ". region:" + response.getBody().picInfo.region + ". requestId: " + response.getBody().requestId); } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getData()); System.out.println(e.getMessage()); e.printStackTrace(); } } }Contoh respons
success: true. message: success. categoryId: 9. region:383,681,291,549. requestId: 016D63E3-D0C5-42D3-8879-55E1A705FBA4
SearchImageByPic
Kode contoh
import com.aliyun.imagesearch20201214.Client; import com.aliyun.teaopenapi.models.Config; import com.aliyun.imagesearch20201214.models.SearchImageByPicAdvanceRequest; import com.aliyun.imagesearch20201214.models.SearchImageByPicResponse; import com.aliyun.tea.TeaException; import com.aliyun.teautil.models.RuntimeOptions; import java.io.FileInputStream; import java.net.URL; import java.io.InputStream; import java.util.List; public class SearchImageByPic { public static void main(String[] args) throws Exception { Config authConfig = new Config(); // For information about how to create an AccessKey pair, see "Create an AccessKey pair" at https://www.alibabacloud.com/help/en/resource-access-management/latest/accesskey-pairs-create-an-accesskey-pair-for-a-ram-user. // The AccessKey pair of an Alibaba Cloud account has the permissions to call all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. // We recommend that you do not hard code your AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account is compromised. // In this example, the AccessKey ID and AccessKey secret are saved as environment variables. You can also save the AccessKey pair in the configuration file based on your business requirements. authConfig.accessKeyId = System.getenv("CC_AK_ENV"); authConfig.accessKeySecret = System.getenv("CC_SK_ENV"); authConfig.type = "access_key"; authConfig.regionId = "ap-southeast-1"; // Replace the value with the ID of the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the regionId parameter to ap-southeast-1. authConfig.endpoint = "imagesearch.ap-southeast-1.aliyuncs.com"; // Replace the value with the endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch.ap-southeast-1.aliyuncs.com. // The following sample code provides an example on how to use a VPC endpoint to call the Image Search API. // Note: You can use a VPC endpoint to call the Image Search API only from ECS instances or resources that reside in the same region. For example, if your Image Search instance resides in Singapore, you can use a VPC endpoint to call the Image Search API only from the ECS instances or resources that reside in Singapore. Otherwise, the API operation fails to be called. In case of a failed API call, check whether your ECS instances or resources are in the same region as your Image Search instance before you call the Image Search API. // authConfig.endpointType = "internal"; // If you want to use a VPC endpoint to call the Image Search API, you must specify the endpointType parameter and set the value to internal. // authConfig.endpoint = "imagesearch-vpc.ap-southeast-1.aliyuncs.com"; // The VPC endpoint that is used to call the Image Search API. Replace the value with the VPC endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch-vpc.ap-southeast-1.aliyuncs.com. Client client = new Client(authConfig); SearchImageByPicAdvanceRequest request = new SearchImageByPicAdvanceRequest(); // Required. The name of the Image Search instance. Enter the instance name instead of the instance ID. After an Image Search instance is purchased, you can view the instance name on the instance list page of the Image Search console. request.instanceName = "XXXXXXXXX"; // Optional. The ID of the product category. // 1. Product image search: If a category is specified, the specified category prevails. If no category is specified, the system predicts and selects a category. The category selected by the system is included in the response. // 2. Generic image search: The category ID is set to 88888888 regardless of whether a category is specified. request.categoryId = 3; // Optional. The number of entries to be returned. Valid values: 1 to 100. Default value: 10. request.num = 10; // Optional. The ordinal number of the first entry to be returned. Valid values: 0 to 499. Default value: 0. request.start = 0; // Optional. Specifies whether to identify the subject in the image. Default value: true. // 1. If you set this parameter to true, the system identifies the subject in the image and searches for images based on the identified subject. The response includes the identification result. // 2. If you set this parameter to false, the system searches for images based on the entire image without subject identification. request.crop = true; // Optional. The subject area of the image, in the format of x1,x2,y1,y2. Specifically, x1 and y1 specify the upper-left point, and x2 and y2 specify the lower-right point. The specified area cannot cross the boundary of the image. // If you specify the region parameter, the search is conducted based on the value of the region parameter regardless of the value of the crop parameter. request.region="167,467,221,407"; // Optional. The filter conditions. // int_attr supports the following operators: >, >=, <, <=, and =. str_attr supports the = and != operators. You can set the logical operator between conditions to AND or OR. // Examples: // 1. Filter images based on the attribute of the INT type: int_attr>=100. // 2. Filter images based on the attribute of the STRING type: str_attr!="value1". // 3. Filter images based on the attributes of the INT and STRING types: int_attr=1000 AND str_attr="value1". request.filter="int_attr=56 AND str_attr=\"test\""; String picName = "D:/123.jpg"; // The image. The image cannot exceed 4 MB in size. The transmission timeout period cannot exceed 5 seconds. Only the following image formats are supported: PNG, JPG, JPEG, BMP, GIF, WebP, TIFF, and PPM. // For product and generic image searches, the length and the width of an image must range from 100 pixels to 4,096 pixels. // The image cannot contain rotation properties. request.num = 10; request.start = 0; // Optional. If the value is set to true, the response data is returned based on the ProductId parameter. request.distinctProductId=false; RuntimeOptions runtimeObject = new RuntimeOptions(); try { // The following sample code provides an example on how to specify an image URL: // String url = "https://www.example.com/123.jpg"; // request.picContentObject = new URL(url).openStream(); // The following sample code provides an example on how to upload an image file: InputStream inputStream = new FileInputStream(picName); request.picContentObject = inputStream; SearchImageByPicResponse response = client.searchImageByPicAdvance(request, runtimeObject); System.out.println(response.getBody().requestId); System.out.println(response.getBody().picInfo.categoryId); System.out.println(response.getBody().picInfo.region); List<SearchImageByPicResponseBody.SearchImageByPicResponseBodyAuctions> auctions = response.getBody().getAuctions(); for(SearchImageByPicResponseBody.SearchImageByPicResponseBodyAuctions auction:auctions) { System.out.println(auction.categoryId + " " + auction.picName + " " + auction.productId + " " + auction.customContent + " " + auction.score + " " + auction.strAttr + " " + auction.intAttr); } System.out.println("Multi-subject information"); SearchImageByPicResponseBody.SearchImageByPicResponseBodyPicInfo picInfo = response.getBody().getPicInfo(); for (SearchImageByPicResponseBody.SearchImageByPicResponseBodyPicInfoMultiRegion multiRegion : picInfo.getMultiRegion()) { System.out.println(multiRegion.region); } } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getData()); System.out.println(e.getMessage()); e.printStackTrace(); } } }Contoh respons
09BE019A-AE3D-4D22-99C9-10533D8AC631 3 167,467,221,407 3 1000 test this is a simple test 1.0 test 56 Informasi multi-subjek 112,440,76,387
SearchImageByName
Kode contoh
import com.aliyun.imagesearch20201214.Client; import com.aliyun.teaopenapi.models.Config; import com.aliyun.imagesearch20201214.models.SearchImageByNameRequest; import com.aliyun.imagesearch20201214.models.SearchImageByNameResponse; import com.aliyun.tea.TeaException; import java.util.List; public class SearchImageByName{ public static void main(String[] args) throws Exception { // Konfigurasi. Config authConfig = new Config(); // For information about how to create an AccessKey pair, see "Create an AccessKey pair" at https://www.alibabacloud.com/help/en/resource-access-management/latest/accesskey-pairs-create-an-accesskey-pair-for-a-ram-user. // The AccessKey pair of an Alibaba Cloud account has the permissions to call all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. // We recommend that you do not hard code your AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account is compromised. // In this example, the AccessKey ID and AccessKey secret are saved as environment variables. You can also save the AccessKey pair in the configuration file based on your business requirements. authConfig.accessKeyId = System.getenv("CC_AK_ENV"); authConfig.accessKeySecret = System.getenv("CC_SK_ENV"); authConfig.type = "access_key"; authConfig.regionId = "ap-southeast-1"; // Replace the value with the ID of the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the regionId parameter to ap-southeast-1. authConfig.endpoint = "imagesearch.ap-southeast-1.aliyuncs.com"; // Replace the value with the endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch.ap-southeast-1.aliyuncs.com. // The following sample code provides an example on how to use a VPC endpoint to call the Image Search API. // Note: You can use a VPC endpoint to call the Image Search API only from ECS instances or resources that reside in the same region. For example, if your Image Search instance resides in Singapore, you can use a VPC endpoint to call the Image Search API only from the ECS instances or resources that reside in Singapore. Otherwise, the API operation fails to be called. In case of a failed API call, check whether your ECS instances or resources are in the same region as your Image Search instance before you call the Image Search API. // authConfig.endpointType = "internal"; // If you want to use a VPC endpoint to call the Image Search API, you must specify the endpointType parameter and set the value to internal. // authConfig.endpoint = "imagesearch-vpc.ap-southeast-1.aliyuncs.com"; // The VPC endpoint that is used to call the Image Search API. Replace the value with the VPC endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch-vpc.ap-southeast-1.aliyuncs.com. Client client = new Client(authConfig); SearchImageByNameRequest request = new SearchImageByNameRequest(); // Required. The name of the Image Search instance. Enter the instance name instead of the instance ID. After an Image Search instance is purchased, you can view the instance name on the instance list page of the Image Search console. request.instanceName = "XXXXXXXXX"; // Required. The ID of the product. The ID cannot exceed 256 characters in length. // A product ID can correspond to multiple image names. request.productId = "test"; // Required. The image name. The name can be up to 256 characters in length. // An image is uniquely identified by the values of the productId and picName parameters. request.picName = "1000"; // Optional. The ID of the product category. // 1. Product image search: If a category is specified, the specified category prevails. If no category is specified, the system predicts and selects a category. The category selected by the system is included in the response. // 2. Generic image search: The category ID is set to 88888888 regardless of whether a category is specified. request.categoryId = 3; // Optional. The number of entries to be returned. Valid values: 1 to 100. Default value: 10. request.num =10; // Optional. The ordinal number of the first entry to be returned. Valid values: 0 to 499. Default value: 0. request.start =0; // Optional. The filter conditions. // int_attr supports the following operators: >, >=, <, <=, and =. str_attr supports the = and != operators. You can set the logical operator between conditions to AND or OR. // Examples: // 1. Filter images based on the attribute of the INT type: int_attr>=100. // 2. Filter images based on the attribute of the STRING type: str_attr!="value1". // 3. Filter images based on the attributes of the INT and STRING types: int_attr=1000 AND str_attr="value1". request.filter = "int_attr=56 AND str_attr =\"test\""; try { SearchImageByNameResponse response = client.searchImageByName(request); System.out.println(response.getBody().requestId); System.out.println(response.getBody().picInfo.categoryId); System.out.println(response.getBody().picInfo.region); List<SearchImageByNameResponseBody.SearchImageByNameResponseBodyAuctions> auctions = response.getBody().getAuctions(); for(SearchImageByNameResponseBody.SearchImageByNameResponseBodyAuctions auction:auctions) { System.out.println(auction.categoryId + " " + auction.picName + " " + auction.productId + " " + auction.customContent + " " + auction.score + " " + auction.strAttr + " " + auction.intAttr); } } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getData()); System.out.println(e.getMessage()); e.printStackTrace(); } } }Contoh respons
8F2441C8-EA05-461B-A4AC-5F5DE25FAC21 3 null 3 1000 test this is a simple test 1.0 test 56
Hapus
Kode contoh
import com.aliyun.imagesearch20201214.Client; import com.aliyun.teaopenapi.models.Config; import com.aliyun.imagesearch20201214.models.DeleteImageRequest; import com.aliyun.imagesearch20201214.models.DeleteImageResponse; import com.aliyun.tea.TeaException; public class Delete { public static void main(String[] args) throws Exception { Config authConfig = new Config(); // Untuk informasi tentang cara membuat pasangan AccessKey, lihat "Buat pasangan AccessKey" di https://www.alibabacloud.com/help/en/resource-access-management/latest/accesskey-pairs-create-an-accesskey-pair-for-a-ram-user. // Pasangan AccessKey akun Alibaba Cloud memiliki izin untuk memanggil semua operasi API. Kami merekomendasikan Anda menggunakan pasangan AccessKey dari pengguna RAM untuk memanggil operasi API atau melakukan O&M rutin. // Kami merekomendasikan agar Anda tidak menuliskan ID AccessKey dan Rahasia AccessKey secara langsung dalam kode proyek Anda. Jika tidak, pasangan AccessKey mungkin bocor dan keamanan semua sumber daya dalam akun Anda terancam. // Dalam contoh ini, ID AccessKey dan Rahasia AccessKey disimpan sebagai variabel lingkungan. Anda juga dapat menyimpan pasangan AccessKey dalam file konfigurasi sesuai dengan kebutuhan bisnis Anda. authConfig.accessKeyId = System.getenv("CC_AK_ENV"); authConfig.accessKeySecret = System.getenv("CC_SK_ENV"); authConfig.type = "access_key"; authConfig.regionId = "ap-southeast-1"; // Ganti nilainya dengan ID wilayah tempat instance Image Search Anda berada. Sebagai contoh, jika instance Image Search Anda berada di Singapura, atur parameter regionId menjadi ap-southeast-1. authConfig.endpoint = "imagesearch.ap-southeast-1.aliyuncs.com"; // Ganti nilainya dengan titik akhir di wilayah tempat instance Image Search Anda berada. Sebagai contoh, jika instance Image Search Anda berada di Singapura, atur parameter endpoint menjadi imagesearch.ap-southeast-1.aliyuncs.com. // Kode sampel berikut memberikan contoh tentang cara menggunakan titik akhir VPC untuk memanggil API Image Search. // Catatan: Anda hanya dapat menggunakan titik akhir VPC untuk memanggil API Image Search dari instance ECS atau sumber daya yang berada di wilayah yang sama. Sebagai contoh, jika instance Image Search Anda berada di Singapura, Anda hanya dapat menggunakan titik akhir VPC untuk memanggil API Image Search dari instance ECS atau sumber daya yang berada di Singapura. Jika tidak, pemanggilan operasi API gagal. Jika pemanggilan API gagal, periksa apakah instance ECS atau sumber daya Anda berada di wilayah yang sama dengan instance Image Search Anda sebelum memanggil API Image Search. // authConfig.endpointType = "internal"; // Jika Anda ingin menggunakan titik akhir VPC untuk memanggil API Image Search, Anda harus menentukan parameter endpointType dan mengatur nilainya menjadi internal. // authConfig.endpoint = "imagesearch-vpc.ap-southeast-1.aliyuncs.com"; // Titik akhir VPC yang digunakan untuk memanggil API Image Search. Ganti nilainya dengan titik akhir VPC di wilayah tempat instance Image Search Anda berada. Sebagai contoh, jika instance Image Search Anda berada di Singapura, atur parameter endpoint menjadi imagesearch-vpc.ap-southeast-1.aliyuncs.com. Client client = new Client(authConfig); DeleteImageRequest request = new DeleteImageRequest(); // Wajib. Nama instance Image Search. Masukkan nama instance alih-alih ID instance. Setelah instance Image Search dibeli, Anda dapat melihat nama instance di halaman daftar instance konsol Image Search. request.instanceName = "XXXXXXXXXXXX"; // Wajib jika parameter isDeleteByFilter diatur ke false. ID produk dapat mencapai panjang maksimal 512 karakter. // ID produk dapat sesuai dengan beberapa nama gambar. Jika Anda hanya menentukan parameter ini dan tidak menentukan parameter picName, respons akan mencakup nama semua gambar yang dihapus. request.productId = "test"; // Opsional. Nama gambar. Jika Anda tidak menyetel parameter ini, sistem menghapus semua gambar yang sesuai dengan ID produk yang ditentukan. Jika Anda menyetel parameter ini, sistem hanya menghapus gambar yang ditentukan oleh parameter product_id dan pic_name. request.picName = "1000"; // Opsional. Jika nilainya diatur ke true, gambar dihapus berdasarkan filter. request.isDeleteByFilter=false; request.filter = "intattr3=xxx"; try { DeleteImageResponse response = client.deleteImage(request); System.out.println(response.getBody().toMap()); } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getData()); System.out.println(e.getMessage()); e.printStackTrace(); } } }Contoh respons
{ Message=success, RequestId=994A7095-09AA-127A-873B-A2279261605F, Data={ PicNames=[ 82.png ] }, Code=0, Success=true }
UpdateImage
Kode contoh
import com.aliyun.imagesearch20201214.Client; import com.aliyun.teaopenapi.models.Config; import com.aliyun.imagesearch20201214.models.*; import com.aliyun.tea.TeaException; import com.aliyun.teautil.models.RuntimeOptions; import java.io.FileInputStream; import java.io.InputStream; import java.util.List; public class UpdateImage { public static void main(String[] args) throws Exception { Config authConfig = new Config(); // For information about how to create an AccessKey pair, see "Create an AccessKey pair" at https://www.alibabacloud.com/help/en/resource-access-management/latest/accesskey-pairs-create-an-accesskey-pair-for-a-ram-user. // The AccessKey pair of an Alibaba Cloud account has the permissions to call all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. // We recommend that you do not hard code your AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account is compromised. // In this example, the AccessKey ID and AccessKey secret are saved as environment variables. You can also save the AccessKey pair in the configuration file based on your business requirements. authConfig.accessKeyId = System.getenv("CC_AK_ENV"); authConfig.accessKeySecret = System.getenv("CC_SK_ENV"); authConfig.type = "access_key"; authConfig.regionId = "ap-southeast-1"; // Replace the value with the ID of the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the regionId parameter to ap-southeast-1. authConfig.endpoint = "imagesearch.ap-southeast-1.aliyuncs.com"; // Replace the value with the endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch.ap-southeast-1.aliyuncs.com. // The following sample code provides an example on how to use a VPC endpoint to call the Image Search API. // Note: You can use a VPC endpoint to call the Image Search API only from ECS instances or resources that reside in the same region. For example, if your Image Search instance resides in Singapore, you can use a VPC endpoint to call the Image Search API only from the ECS instances or resources that reside in Singapore. Otherwise, the API operation fails to be called. In case of a failed API call, check whether your ECS instances or resources are in the same region as your Image Search instance before you call the Image Search API. // authConfig.endpointType = "internal"; // If you want to use a VPC endpoint to call the Image Search API, you must specify the endpointType parameter and set the value to internal. // authConfig.endpoint = "imagesearch-vpc.ap-southeast-1.aliyuncs.com"; // The VPC endpoint that is used to call the Image Search API. Replace the value with the VPC endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch-vpc.ap-southeast-1.aliyuncs.com. Client client = new Client(authConfig); UpdateImageRequest request = new UpdateImageRequest(); // Required. The name of the Image Search instance. Enter the instance name instead of the instance ID. After an Image Search instance is purchased, you can view the instance name on the instance list page of the Image Search console. request.instanceName = "XXXXXXXXX"; // Required. The ID of the product. The product ID cannot be changed. request.setProductId("1"); // Required. The image name. The image name cannot be changed. request.setPicName("1"); // Optional. The attribute of the INT type. The attribute can be used to filter images during image searches. If you specify this parameter, the response includes this parameter and its value. request.setIntAttr(1); // Optional. The attribute of the STRING type. The value can be up to 128 characters in length. The attribute can be used to filter images during image searches. If you specify this parameter, the response includes this parameter and its value. request.setStrAttr("test"); // Optional. The user-defined content. The value can be up to 4,096 characters in length. request.setCustomContent("This is a sample description"); UpdateImageResponse response = client.updateImage(request); System.out.println("requestId: " + response.getBody().requestId + ". success: " + response.getBody().success + ". message: " + response.getBody().message); } }Contoh respons
requestId: AB8AA177-89AB-14D5-BEE0-293C32D5C43A. success: true. message: null
Detail
Kode contoh
import com.aliyun.imagesearch20201214.Client; import com.aliyun.teaopenapi.models.Config; import com.aliyun.imagesearch20201214.models.*; import com.aliyun.tea.TeaException; import com.aliyun.teautil.models.RuntimeOptions; import java.io.FileInputStream; import java.io.InputStream; import java.util.List; public class Detail { public static void main(String[] args) throws Exception { Config authConfig = new Config(); // For information about how to create an AccessKey pair, see "Create an AccessKey pair" at https://www.alibabacloud.com/help/en/resource-access-management/latest/accesskey-pairs-create-an-accesskey-pair-for-a-ram-user. // The AccessKey pair of an Alibaba Cloud account has the permissions to call all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. // We recommend that you do not hard code your AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account is compromised. // In this example, the AccessKey ID and AccessKey secret are saved as environment variables. You can also save the AccessKey pair in the configuration file based on your business requirements. authConfig.accessKeyId = System.getenv("CC_AK_ENV"); authConfig.accessKeySecret = System.getenv("CC_SK_ENV"); authConfig.type = "access_key"; authConfig.regionId = "ap-southeast-1"; // Replace the value with the ID of the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the regionId parameter to ap-southeast-1. authConfig.endpoint = "imagesearch.ap-southeast-1.aliyuncs.com"; // Replace the value with the endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch.ap-southeast-1.aliyuncs.com. // The following sample code provides an example on how to use a VPC endpoint to call the Image Search API. // Note: You can use a VPC endpoint to call the Image Search API only from ECS instances or resources that reside in the same region. For example, if your Image Search instance resides in Singapore, you can use a VPC endpoint to call the Image Search API only from the ECS instances or resources that reside in Singapore. Otherwise, the API operation fails to be called. In case of a failed API call, check whether your ECS instances or resources are in the same region as your Image Search instance before you call the Image Search API. // authConfig.endpointType = "internal"; // If you want to use a VPC endpoint to call the Image Search API, you must specify the endpointType parameter and set the value to internal. // authConfig.endpoint = "imagesearch-vpc.ap-southeast-1.aliyuncs.com"; // The VPC endpoint that is used to call the Image Search API. Replace the value with the VPC endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch-vpc.ap-southeast-1.aliyuncs.com. Client client = new Client(authConfig); DetailRequest request = new DetailRequest(); // Required. The name of the Image Search instance. Enter the instance name instead of the instance ID. After an Image Search instance is purchased, you can view the instance name on the instance list page of the Image Search console. request.instanceName = "XXXXXXXXX"; DetailResponse detail = client.detail(request); System.out.println(detail.getBody().toMap()); } }Contoh respons
{ RequestId=046913C5-942A-1DD2-959F-7CECD790ADB2, Instance={ UtcExpireTime=1649692800000, TotalCount=225320, UtcCreate=1633936585000, Capacity=250, Qps=5, ServiceType=0, Region=cn-shanghai, Name=xxxx }, Success=true }
DumpMeta
Kode contoh
import com.aliyun.imagesearch20201214.Client; import com.aliyun.teaopenapi.models.Config; import com.aliyun.imagesearch20201214.models.*; import com.aliyun.tea.TeaException; import com.aliyun.teautil.models.RuntimeOptions; import java.io.FileInputStream; import java.io.InputStream; import java.util.List; public class DumpMeta { public static void main(String[] args) throws Exception { Config authConfig = new Config(); // For information about how to create an AccessKey pair, see "Create an AccessKey pair" at https://www.alibabacloud.com/help/en/resource-access-management/latest/accesskey-pairs-create-an-accesskey-pair-for-a-ram-user. // The AccessKey pair of an Alibaba Cloud account has the permissions to call all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. // We recommend that you do not hard code your AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account is compromised. // In this example, the AccessKey ID and AccessKey secret are saved as environment variables. You can also save the AccessKey pair in the configuration file based on your business requirements. authConfig.accessKeyId = System.getenv("CC_AK_ENV"); authConfig.accessKeySecret = System.getenv("CC_SK_ENV"); authConfig.type = "access_key"; authConfig.regionId = "ap-southeast-1"; // Replace the value with the ID of the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the regionId parameter to ap-southeast-1. authConfig.endpoint = "imagesearch.ap-southeast-1.aliyuncs.com"; // Replace the value with the endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch.ap-southeast-1.aliyuncs.com. // The following sample code provides an example on how to use a VPC endpoint to call the Image Search API. // Note: You can use a VPC endpoint to call the Image Search API only from ECS instances or resources that reside in the same region. For example, if your Image Search instance resides in Singapore, you can use a VPC endpoint to call the Image Search API only from the ECS instances or resources that reside in Singapore. Otherwise, the API operation fails to be called. In case of a failed API call, check whether your ECS instances or resources are in the same region as your Image Search instance before you call the Image Search API. // authConfig.endpointType = "internal"; // If you want to use a VPC endpoint to call the Image Search API, you must specify the endpointType parameter and set the value to internal. // authConfig.endpoint = "imagesearch-vpc.ap-southeast-1.aliyuncs.com"; // The VPC endpoint that is used to call the Image Search API. Replace the value with the VPC endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch-vpc.ap-southeast-1.aliyuncs.com. Client client = new Client(authConfig); DumpMetaRequest request = new DumpMetaRequest(); // Required. The name of the Image Search instance. Enter the instance name instead of the instance ID. After an Image Search instance is purchased, you can view the instance name on the instance list page of the Image Search console. request.instanceName = "XXXXXXXXX"; DumpMetaResponse response = client.dumpMeta(request); System.out.println(response.getBody().toMap()); } }Contoh respons
{ RequestId=FC4191AA-1D5B-1001-9A70-18FBB2BD265B, Data={ DumpMetaStatus=PROCESSING, Id=567 }, Success=true }
DumpMetaList
Kode contoh
import com.aliyun.imagesearch20201214.Client; import com.aliyun.teaopenapi.models.Config; import com.aliyun.imagesearch20201214.models.*; import com.aliyun.tea.TeaException; import com.aliyun.teautil.models.RuntimeOptions; import java.io.FileInputStream; import java.io.InputStream; import java.util.List; public class DumpMetaList { public static void main(String[] args) throws Exception { Config authConfig = new Config(); // For information about how to create an AccessKey pair, see "Create an AccessKey pair" at https://www.alibabacloud.com/help/en/resource-access-management/latest/accesskey-pairs-create-an-accesskey-pair-for-a-ram-user. // The AccessKey pair of an Alibaba Cloud account has the permissions to call all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. // We recommend that you do not hard code your AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account is compromised. // In this example, the AccessKey ID and AccessKey secret are saved as environment variables. You can also save the AccessKey pair in the configuration file based on your business requirements. authConfig.accessKeyId = System.getenv("CC_AK_ENV"); authConfig.accessKeySecret = System.getenv("CC_SK_ENV"); authConfig.type = "access_key"; authConfig.regionId = "ap-southeast-1"; // Replace the value with the ID of the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the regionId parameter to ap-southeast-1. authConfig.endpoint = "imagesearch.ap-southeast-1.aliyuncs.com"; // Replace the value with the endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch.ap-southeast-1.aliyuncs.com. // The following sample code provides an example on how to use a VPC endpoint to call the Image Search API. // Note: You can use a VPC endpoint to call the Image Search API only from ECS instances or resources that reside in the same region. For example, if your Image Search instance resides in Singapore, you can use a VPC endpoint to call the Image Search API only from the ECS instances or resources that reside in Singapore. Otherwise, the API operation fails to be called. In case of a failed API call, check whether your ECS instances or resources are in the same region as your Image Search instance before you call the Image Search API. // authConfig.endpointType = "internal"; // If you want to use a VPC endpoint // authConfig.endpointType = "internal"; // Jika menggunakan layanan pencarian gambar melalui jaringan internal, endpointType wajib diisi, nilainya selalu "internal" // authConfig.endpoint = "imagesearch-vpc.ap-southeast-1.aliyuncs.com"; // Alamat akses jaringan internal (VPC), silakan ganti dengan wilayah tempat Anda membeli instance, misalnya jika wilayah pembelian adalah Singapura, maka endpoint="imagesearch-vpc.ap-southeast-1.aliyuncs.com" Client client = new Client(authConfig); DumpMetaListRequest request = new DumpMetaListRequest(); // Wajib diisi, nama instance pencarian gambar. Perhatikan bahwa ini adalah nama instance, bukan ID instance. Setelah pembelian, Anda dapat melihat nama instance di halaman informasi instance konsol pencarian gambar. request.instanceName = "XXXXXXXXX"; // Opsional, ID tugas yang digunakan untuk mengekspor metadata. request.setId(567L); // Opsional, nomor halaman. Nilai default: 1. request.setPageNumber(1); // Opsional, jumlah entri per halaman. Nilai default: 20. request.setPageSize(1); DumpMetaListResponse response = client.dumpMetaList(request); System.out.println(response.getBody().toMap()); } }Contoh respons
{ RequestId=850DFBD9-A179-12FB-B193-2D08ACEA586B, Data={ TotalCount=1, PageSize=1, PageNumber=1, DumpMetaList=[ { Status=SUCCESS, Msg=berhasil, MetaUrl=https://example.com/x?Expires=x, UtcCreate=1.639.969.113.000, UtcModified=1.639.969.140.000, Id=567, Code=0 } ] } }
BatchTask
Kode contoh
import com.aliyun.imagesearch20201214.Client; import com.aliyun.teaopenapi.models.Config; import com.aliyun.imagesearch20201214.models.*; import com.aliyun.tea.TeaException; import com.aliyun.teautil.models.RuntimeOptions; import java.io.FileInputStream; import java.io.InputStream; import java.util.List; public class BatchTask { public static void main(String[] args) throws Exception { Config authConfig = new Config(); // Untuk informasi tentang cara membuat pasangan AccessKey, lihat "Buat pasangan AccessKey" di https://www.alibabacloud.com/help/en/resource-access-management/latest/accesskey-pairs-create-an-accesskey-pair-for-a-ram-user. // Pasangan AccessKey akun Alibaba Cloud memiliki izin untuk memanggil semua operasi API. Kami menyarankan Anda menggunakan pasangan AccessKey pengguna RAM untuk memanggil operasi API atau melakukan O&M rutin. // Kami menyarankan agar Anda tidak mengkode keras ID AccessKey dan Rahasia AccessKey Anda dalam kode proyek Anda. Jika tidak, pasangan AccessKey mungkin bocor dan keamanan semua sumber daya dalam akun Anda terancam. // Dalam contoh ini, ID AccessKey dan Rahasia AccessKey disimpan sebagai variabel lingkungan. Anda juga dapat menyimpan pasangan AccessKey dalam file konfigurasi berdasarkan kebutuhan bisnis Anda. authConfig.accessKeyId = System.getenv("CC_AK_ENV"); authConfig.accessKeySecret = System.getenv("CC_SK_ENV"); authConfig.type = "access_key"; authConfig.regionId = "ap-southeast-1"; // Ganti nilainya dengan ID wilayah tempat instance Image Search Anda berada. Misalnya, jika instance Image Search Anda berada di Singapura, atur parameter regionId menjadi ap-southeast-1. authConfig.endpoint = "imagesearch.ap-southeast-1.aliyuncs.com"; // Ganti nilainya dengan titik akhir di wilayah tempat instance Image Search Anda berada. Misalnya, jika instance Image Search Anda berada di Singapura, atur parameter titik akhir menjadi imagesearch.ap-southeast-1.aliyuncs.com. // Kode sampel berikut memberikan contoh tentang cara menggunakan titik akhir VPC untuk memanggil API Image Search. // Catatan: Anda hanya dapat menggunakan titik akhir VPC untuk memanggil API Image Search dari instance ECS atau sumber daya yang berada di wilayah yang sama. Misalnya, jika instance Image Search Anda berada di Singapura, Anda hanya dapat menggunakan titik akhir VPC untuk memanggil API Image Search dari instance ECS atau sumber daya yang berada di Singapura. Jika tidak, pemanggilan operasi API gagal. Jika pemanggilan API gagal, periksa apakah instance ECS atau sumber daya Anda berada di wilayah yang sama dengan instance Image Search Anda sebelum Anda memanggil API Image Search. // authConfig.endpointType = "internal"; // Jika Anda ingin menggunakan titik akhir VPC untuk memanggil API Image Search, Anda harus menentukan parameter endpointType dan mengatur nilainya menjadi internal. // authConfig.endpoint = "imagesearch-vpc.ap-southeast-1.aliyuncs.com"; // Titik akhir VPC yang digunakan untuk memanggil API Image Search. Ganti nilainya dengan titik akhir VPC di wilayah tempat instance Image Search Anda berada. Misalnya, jika instance Image Search Anda berada di Singapura, atur parameter titik akhir menjadi imagesearch-vpc.ap-southeast-1.aliyuncs.com. Client client = new Client(authConfig); IncreaseInstanceRequest request = new IncreaseInstanceRequest(); // Wajib. Nama instance Image Search. Masukkan nama instance bukan ID instance. Setelah instance Image Search dibeli, Anda dapat melihat nama instance di halaman daftar instance konsol Image Search. request.instanceName = "XXXXXXXXX"; // Wajib. Jika gambar disimpan di bucket Object Storage Service (OSS), masukkan nama bucket. request.setBucketName("bucketName"); // Wajib. Jalur tempat gambar disimpan. Jalur harus dimulai dengan garis miring (/) dan tidak boleh diakhiri dengan garis miring (/). // Buat file bernama increment.meta di jalur tempat gambar disimpan. Untuk informasi lebih lanjut, lihat "Melakukan operasi batch" di Panduan Pengguna. request.setPath("/public/xxx"); // Opsional. URL panggilan balik jika pemanggilan API berhasil. URL harus dimulai dengan http:// atau https://. request.setCallbackAddress("http://xxx/xxx"); IncreaseInstanceResponse response = client.increaseInstance(request); System.out.println(response.getBody().toMap()); } }Contoh respons
{ RequestId=F9BAD635-3031-1EBB-BE9E-E9FCB318A28C, Data={ IncrementStatus=SEDANG DIPROSES, Id=1470 }, Success=true }
BatchTaskList
Kode contoh
import com.aliyun.imagesearch20201214.Client; import com.aliyun.teaopenapi.models.Config; import com.aliyun.imagesearch20201214.models.*; import com.aliyun.tea.TeaException; import com.aliyun.teautil.models.RuntimeOptions; import java.io.FileInputStream; import java.io.InputStream; import java.util.List; public class BatchTaskList { public static void main(String[] args) throws Exception { Config authConfig = new Config(); // For information about how to create an AccessKey pair, see "Create an AccessKey pair" at https://www.alibabacloud.com/help/en/resource-access-management/latest/accesskey-pairs-create-an-accesskey-pair-for-a-ram-user. // The AccessKey pair of an Alibaba Cloud account has the permissions to call all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. // We recommend that you do not hard code your AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account is compromised. // In this example, the AccessKey ID and AccessKey secret are saved as environment variables. You can also save the AccessKey pair in the configuration file based on your business requirements. authConfig.accessKeyId = System.getenv("CC_AK_ENV"); authConfig.accessKeySecret = System.getenv("CC_SK_ENV"); authConfig.type = "access_key"; authConfig.regionId = "ap-southeast-1"; // Replace the value with the ID of the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the regionId parameter to ap-southeast-1. authConfig.endpoint = "imagesearch.ap-southeast-1.aliyuncs.com"; // Replace the value with the endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch.ap-southeast-1.aliyuncs.com. // The following sample code provides an example on how to use a VPC endpoint to call the Image Search API. // Note: You can use a VPC endpoint to call the Image Search API only from ECS instances or resources that reside in the same region. For example, if your Image Search instance resides in Singapore, you can use a VPC endpoint to call the Image Search API only from the ECS instances or resources that reside in Singapore. Otherwise, the API operation fails to be called. In case of a failed API call, check whether your ECS instances or resources are in the same region as your Image Search instance before you call the Image Search API. // authConfig.endpointType = "internal"; // If you want to use a VPC endpoint to call the Image Search API, you must specify the endpointType parameter and set the value to internal. // authConfig.endpoint = "imagesearch-vpc.ap-southeast-1.aliyuncs.com"; // The VPC endpoint that is used to call the Image Search API. Replace the value with the VPC endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch-vpc.ap-southeast-1.aliyuncs.com. Client client = new Client(authConfig); IncreaseListRequest request = new IncreaseListRequest(); // Required. The name of the Image Search instance. Enter the instance name instead of the instance ID. After an Image Search instance is purchased, you can view the instance name on the instance list page of the Image Search console. request.instanceName = "XXXXXXXXX"; // Optional. The ID of the batch task. request.setId(1470L); // Optional. The name of the OSS bucket. request.setBucketName("BucketName"); // Optional. The path to the OSS bucket. request.setPath("/localization/xxxx"); // Optional. The page number. Default value: 1. request.setPageNumber(1); // Optional. The number of entries per page. Default value: 20. request.setPageSize(1); IncreaseListResponse response = client.increaseList(request); System.out.println(response.getBody().toMap()); } }Contoh respons
{ RequestId=56E7E6CC-64AB-17CA-A7CD-1948FB953B8C, Data={ TotalCount=1, PageSize=1, PageNumber=1, Increments={ Instance=[ { Msg=success, Status=NORMAL, Path=/xx/xx, BucketName=bucketName, UtcCreate=1639107872000, ErrorUrl=https://example.com/xxx?Expires=1670661540&xxx=xx, UtcModified=1639125540000, Id=1464, CallbackAddress=null, Code=0 } ] } } }
CompareSimilarByImage
Kode contoh
package com.aliyun.sample; import com.aliyun.imagesearch20201214.Client; import com.aliyun.imagesearch20201214.models.CompareSimilarByImageAdvanceRequest; import com.aliyun.imagesearch20201214.models.CompareSimilarByImageResponse; import com.aliyun.tea.TeaException; import com.aliyun.teautil.models.RuntimeOptions; import com.aliyun.teaopenapi.models.Config; import java.io.FileInputStream; import java.io.InputStream; public class CompareSimilarByImage { public static void main(String[] args) throws Exception { Config authConfig = new Config(); // For information about how to create an AccessKey pair, see "Create an AccessKey pair" at https://www.alibabacloud.com/help/en/resource-access-management/latest/accesskey-pairs-create-an-accesskey-pair-for-a-ram-user. // The AccessKey pair of an Alibaba Cloud account has the permissions to call all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M. // We recommend that you do not hard code your AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account is compromised. // In this example, the AccessKey ID and AccessKey secret are saved as environment variables. You can also save the AccessKey pair in the configuration file based on your business requirements. authConfig.accessKeyId = System.getenv("CC_AK_ENV"); authConfig.accessKeySecret = System.getenv("CC_SK_ENV"); authConfig.type = "access_key"; authConfig.regionId = "ap-southeast-1"; // Replace the value with the ID of the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the regionId parameter to ap-southeast-1. authConfig.endpoint = "imagesearch.ap-southeast-1.aliyuncs.com"; // Replace the value with the endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch.ap-southeast-1.aliyuncs.com. // The following sample code provides an example on how to use a virtual private cloud (VPC) endpoint to call the Image Search API. // Note: You can use a VPC endpoint to call the Image Search API only from Elastic Compute Service (ECS) instances or resources that reside in the same region. For example, if your Image Search instance resides in Singapore, you can use a VPC endpoint to call the Image Search API only from the ECS instances or resources that reside in Singapore. Otherwise, the API operation fails to be called. In case of a failed API call, check whether your ECS instances or resources are in the same region as your Image Search instance before you call the Image Search API. // authConfig.endpointType = "internal"; // If you want to use a VPC endpoint to call the Image Search API, you must specify the endpointType parameter and set the value to internal. // authConfig.endpoint = "imagesearch-vpc.ap-southeast-1.aliyuncs.com"; // The VPC endpoint that is used to call the Image Search API. Replace the value with the VPC endpoint in the region in which your Image Search instance resides. For example, if your Image Search instance resides in Singapore, set the endpoint parameter to imagesearch-vpc.ap-southeast-1.aliyuncs.com. Client client = new Client(authConfig); CompareSimilarByImageAdvanceRequest compareSimilarByImageAdvanceRequest = new CompareSimilarByImageAdvanceRequest(); // Required. The name of the Image Search instance. Enter the instance name instead of the instance ID. After an Image Search instance is purchased, you can view the instance name on the instance list page of the Image Search console. compareSimilarByImageAdvanceRequest.setInstanceName("XXXXXXXXX"); String primaryName = "D:/1.jpg"; String secondaryName = "D:/2.jpg"; try { // The following sample code provides an example on how to specify an image URL: // String primaryUrl = "https://www.example.com/1.jpg"; // String secondaryUrl = "https://www.example.com/2.jpg"; // compareSimilarByImageAdvanceRequest.setPrimaryPicContentObject(new URL(primaryUrl).openStream()); // compareSimilarByImageAdvanceRequest.setSecondaryPicContentObject(new URL(secondaryUrl).openStream()); // The following sample code provides an example on how to upload an image file: InputStream primaryInputStream = new FileInputStream(primaryName); InputStream secondaryInputStream = new FileInputStream(secondaryName); compareSimilarByImageAdvanceRequest.setPrimaryPicContentObject(primaryInputStream); compareSimilarByImageAdvanceRequest.setSecondaryPicContentObject(secondaryInputStream); RuntimeOptions runtimeOptions = new RuntimeOptions(); CompareSimilarByImageResponse compareSimilarByImageResponse = client.compareSimilarByImageAdvance(compareSimilarByImageAdvanceRequest, runtimeOptions); System.out.println(compareSimilarByImageResponse.getBody().toMap()); } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getData()); System.out.println(e.getMessage()); e.printStackTrace(); } } } }Contoh respons
{ Msg=success, Score=1.0, RequestId=5C0F29D6-D5DA-1068-9A6A-F68B07F8E02F, Code=200, Success=true }