实际上,在前几天的blog,我们有从压缩方式、图像质量、透明效果、可编辑性等各方面做过差异化的介绍。
Android平台RTSP|RTMP播放器截图,我们都是直接保存png图片,新的接口,jpg和png均支持。
原接口:
/**
* 请使用新的CaptureImage接口, 这个接口只能保存为PNG图片, 不再推荐使用
* Save current image during playback stream(实时快照)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param imageName: image name, which including fully path, "/sdcard/daniuliveimage/daniu.png", etc.
*
* @return {0} if successful
*/
public native int SmartPlayerSaveCurImage(long handle, String imageName);
值得注意的是,原接口如果需要截图,还需要调用SmartPlayerSaveImageFlag()。
新的接口,我们设计如下:
/**
* 新的截图接口, 支持JPEG和PNG两种格式
* @param compress_format: 压缩格式, 0:JPEG格式, 1:PNG格式, 其他返回错误
* @param quality: 取值范围:[0, 100], 值越大图像质量越好, 仅对JPEG格式有效, 若是PNG格式,请填100
* @param file_name: 图像文件名, 例如:/dirxxx/test20231113100739.jpeg, /dirxxx/test20231113100739.png
* @param user_data_string: 用户自定义字符串
* @return {0} if successful
*/
public native int CaptureImage(long handle, int compress_format, int quality, String file_name, String user_data_string);
btnCaptureImage.setOnClickListener(new Button.OnClickListener() {
@SuppressLint("SimpleDateFormat")
public void onClick(View v) {
if (0 == playerHandle)
return;
if (null == capture_image_date_format_)
capture_image_date_format_ = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS");
String timestamp = capture_image_date_format_.format(new Date());
String imageFileName = timestamp;
String image_path = imageSavePath + "/" + imageFileName;
int quality;
boolean is_jpeg = true;
if (is_jpeg) {
image_path += ".jpeg";
quality = 100;
}
else {
image_path += ".png";
quality = 100;
}
int capture_ret = libPlayer.CaptureImage(playerHandle,is_jpeg?0:1, quality, image_path, "test cix");
Log.i(TAG, "capture image ret:" + capture_ret + ", file:" + image_path);
}
});
截图成功,对应的event回调如下:
class EventHandeV2 implements NTSmartEventCallbackV2 {
@Override
public void onNTSmartEventCallbackV2(long handle, int id, long param1,
long param2, String param3, String param4, Object param5) {
String player_event = "";
switch (id) {
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_STARTED:
player_event = "开始..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CONNECTING:
player_event = "连接中..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CONNECTION_FAILED:
player_event = "连接失败..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CONNECTED:
player_event = "连接成功..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_DISCONNECTED:
player_event = "连接断开..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_STOP:
player_event = "停止播放..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_RESOLUTION_INFO:
player_event = "分辨率信息: width: " + param1 + ", height: " + param2;
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_NO_MEDIADATA_RECEIVED:
player_event = "收不到媒体数据,可能是url错误..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_SWITCH_URL:
player_event = "切换播放URL..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CAPTURE_IMAGE:
player_event = "快照: " + param1 + " 路径:" + param3;
if (param1 == 0)
player_event = player_event + ", 截取快照成功";
else
player_event = player_event + ", 截取快照失败";
if (param4 != null && !param4.isEmpty())
player_event += (", user data:" + param4);
break;
..........
}
if (player_event.length() > 0) {
Log.i(TAG, player_event);
Message message = new Message();
message.what = PLAYER_EVENT_MSG;
message.obj = player_event;
handler.sendMessage(message);
}
}
}
大家都知道,JPG(JPEG)是有损压缩格式,保持图片质量的同时,通过牺牲一部分图像信息来减小文件尺寸。所以,JPG适合用于存储照片和其他复杂的图像。JPG格式的文件通常比PNG格式的文件小,因此在网络传输和存储时更为方便。