English
English
简体中文
Contact Us
Register
Log In
layoutIndex

Encrypted Images

Last Updated on : 2022-03-03 08:20:13download

Detection alerts can be enabled for IP cameras (IPCs). In most cases, a real-time video screenshot is uploaded to the cloud if a detection alert is generated. IPC SDK is designed to encrypt this image and attach it to the result of querying an alert message or cloud storage event.

Initialize Fresco

The image decryption component DecryptImageView is an image loading component developed based on Fresco. You must initialize the Fresco class before an image can be loaded.

If other SDKs also use Fresco, it is initialized for the IPC SDK following all other SDKs to avoid data overwriting. Otherwise, encrypted images cannot be displayed as expected.

Example

public class MyApplication extends Application {
	@Override
	public void onCreate() {
		super.onCreate();
        FrescoManager.initFresco(this);
	}
}

Display an encrypted image

API description

public void setImageURI(String url, byte[] key);

Parameters

Parameter Description
url The URL of the image.
key The encryption key.

Example

String encroption = "xxx";
DecryptImageView bg = view.findViewById(R.id.decrypt_image_view);
bg.setImageURI(imgUrl, encroption.getBytes());

Alert images

  • In an alert, the value of the URL for the attached image consists of the image URL and encryption key, which are concatenated in the format of {path}@{key}. To display the encrypted image, this concatenated string is parsed to get the decryption data.
  • If the string is not suffixed with @{key}, the attached image is not encrypted.

Example

val img: SimpleDraweeView = findViewById(R.id.img);
if (mUriString.contains("@")) {
    val index = mUriString.lastIndexOf("@")
    try {
        val decryption = mUriString.substring(index + 1)
        val imageUrl = mUriString.substring(0, index)
        val builder = ImageRequestBuilder.newBuilderWithSource(Uri.parse(imageUrl))
                .setRotationOptions(RotationOptions.autoRotateAtRenderTime())
                .disableDiskCache()
        val imageRequest = DecryptImageRequest(builder, "AES", "AES/CBC/PKCS5Padding",
                decryption.toByteArray())
        controller = Fresco.newDraweeControllerBuilder().setImageRequest(imageRequest)
                .build()
    } catch (e: Exception) {
        e.printStackTrace()
    }
} else {
    try {
        uri = Uri.parse(mUriString)
    } catch (e: Exception) {
        e.printStackTrace()
    }
    val builder = Fresco.newDraweeControllerBuilder().setUri(uri)
    controller = builder.build()
}
img.controller = controller

Cloud storage event images

In cloud storage events, the value of the event screenshot TimeRangeBean.snapshotUrl is the complete URL of the image. The screenshot is decrypted with the unified key for cloud-stored video playback.

Example

String mEncryptKey = "";

@Override
public void getCloudSecret(String encryKey) {
    // Returns the unified key for cloud-stored video playback.
    mEncryptKey = encryKey;
}

@Override
public void getMotionDetectionByTimeSlice(List<TimeRangeBean> list) {
    // Returns motion detection data within a specified period.
    TimeRangeBean timeRangeBean = list.get(0);
    if (timeRangeBean.getV() == 2 && !TextUtils.isEmpty(mEncryptKey)) {
      mSnapshot.setImageURI(timeRangeBean.getSnapshotUrl(), mEncryptKey.getBytes());
    } else {
      mSnapshot.setImageURI(timeRangeBean.getSnapshotUrl());
    }
}

Download an encrypted image

API description

void downloadEncryptedImg(String url, String key, ITuyaResultCallback<Bitmap> callback);

Parameters

Parameter Description
url The URL of the image.
key The encryption key.
callback The callback. A bitmap file is returned on success.

Example

ITuyaIPCTool tool = TuyaIPCSdk.getTool();
if (tool != null) {
    tool.downloadEncryptedImg(url, key, new ITuyaResultCallback<Bitmap>() {
        @Override
        public void onSuccess(Bitmap result) {

        }

        @Override
        public void onError(String errorCode, String errorMessage) {

        }
    });
}