Image Processing (IMG) provided by Object Storage Service (OSS) is a secure, cost-effective, and highly reliable service that can be used to process large numbers of images. After you upload images to OSS, you can call RESTful API operations to process the images from a device that is connected to the Internet anytime, anywhere.
Usage notes
-
Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization (Android SDK).
Usage of IMG
IMG parameters apply only to the downloaded image data and do not affect the source images. To replace source images with processed images, see Save processed images.
Anonymous access
String url = oss.presignPublicObjectURL(testBucket, testObject); OSSLog.logDebug("signPublicURL", "get url: " + url); Then, append the x-oss-process parameter to the URL. The parameter's value specifies the image processing operation.Authorized access
To process an image using the SDK, call the
request.setxOssProcess()method to set the processing parameters for the download request. The following code provides an example:// Create a request to download an image. // Specify the bucket name (for example, examplebucket) and the object full path (for example, exampledir/exampleobject.txt). The object full path cannot contain the bucket name. GetObjectRequest request = new GetObjectRequest("examplebucket", "exampledir/exampleobject.txt"); // Process the image. request.setxOssProcess("image/resize,m_fixed,w_100,h_100"); OSSAsyncTask task = oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() { @Override public void onSuccess(GetObjectRequest request, GetObjectResult result) { // The request is successful. InputStream inputStream = result.getObjectContent(); byte[] buffer = new byte[2048]; int len; try { while ((len = inputStream.read(buffer)) != -1) { // Process the downloaded data. } } catch (IOException e) { e.printStackTrace(); } } @Override public void onFailure(GetObjectRequest request, ClientException clientException, ServiceException serviceException) { // Handle the exception. You can add your own exception handling logic. } });NoteTo perform other operations on the image, replace the argument passed to
request.setxOssProcess().Access by using the OSS SDK
GetObjectRequest request = new GetObjectRequest("bucket-name", "image-name"); request.setxOssProcess("image/resize,m_lfit,w_100,h_100"); // Set image processing parameters. OSSAsyncTask task = oss.asyncGetObject(request, getCallback);
Save processed images
The following sample code provides an example on how to save processed images:
// fromBucket and toBucket specify the source and destination bucket names.
// fromObjectKey and toObjectKey specify the source and destination object names. The names must be full paths that include the file extension, for example, abc/efg/123.jpg.
// action specifies the image processing operation, such as "image/resize,m_lfit,w_100,h_100".
ImagePersistRequest request = new ImagePersistRequest(fromBucket,fromObjectKey,toBucket,toObjectKey,action);
OSSAsyncTask task = oss.asyncImagePersist(request, new OSSCompletedCallback<ImagePersistRequest, ImagePersistResult>() {
@Override
public void onSuccess(ImagePersistRequest request, ImagePersistResult result) {
// Success callback
log.i("info", "Success");
}
@Override
public void onFailure(ImagePersistRequest request, ClientException clientException, ServiceException serviceException) {
// The request failed.
if (clientException != null) {
// A client-side exception occurred, such as a network error.
clientException.printStackTrace();
}
if (serviceException != null) {
// A server-side exception occurred.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});Image processing tool
Use the ImageStyleViewer visual tool to preview the results of Image Processing in real time.
References
For more information about the supported IMG parameters, see IMG parameters.
For the complete sample code that is used to perform IMG operations, visit GitHub.
For more information about how to initialize an OSSClient instance, see Initialization.