Encrypted Images

Last Updated on : 2024-03-04 08:53:15

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 = "";

CloudBusiness cloudBusiness =new CloudBusiness();
   cloudBusiness.getCloudSecret(devId, new Business.ResultListener<JSONObject>() {
            @Override
            public void onFailure(BusinessResponse bizResponse, JSONObject bizResult, String apiName) {

            }

            @Override
            public void onSuccess(BusinessResponse bizResponse, JSONObject bizResult, String apiName) {
                    mEncryptKey = bizResult.getString("encryptKey");
            }
        });

CloudDayBean dayBean = dayBeanList.get(0);
cloudCamera.getMotionDetectionInfo(devId, dayBean.getCurrentStartDayTime(), dayBean.getCurrentDayEndTime(), offset, limit, new IThingResultCallback<List<TimeRangeBean>>() {
@Override
public void onSuccess(List<TimeRangeBean> result) {

        TimeRangeBean timeRangeBean = list.get(0);
        if (timeRangeBean.getV() == 2 && !TextUtils.isEmpty(mEncryptKey)) {
          mSnapshot.setImageURI(timeRangeBean.getSnapshotUrl(),            mEncryptKey.getBytes());
        } else {
          mSnapshot.setImageURI(timeRangeBean.getSnapshotUrl());
        }
      }

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

        }
 });

Download an encrypted image

API description

void downloadEncryptedImg(String url, String key, IThingResultCallback<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

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

        }

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

        }
    });
}