基于 OpenHarmony compress 三方件

作者: 迪士尼在逃程序员 | 来源:发表于2024-08-06 13:37 被阅读0次

关于

提供了一个轻量级的图像压缩库。将允许您将大照片压缩成小
尺寸的照片,图像质量损失或可以忽略不计

compress 的依赖添加

为你的应用添加 compress-debug.har。将 compress-debug.har 复制到 entry\libs 目录下即可(由于 build.gradle 中已经依赖的 libs 目录下的*.har,因此不需要在做修改)。

使用

(1)ability_main.xml 设置界面布局

<DependentLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:width="match_parent"
ohos:height="match_parent"
ohos:background_element="#FFFFFF">
<Image
ohos:id="$+id:image1"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:image_src="$media:dog1.PNG"/>
<Text
ohos:id="$+id:text"

ohos:width="match_content"
ohos:height="match_content"
ohos:text=""
ohos:text_size="19fp"
ohos:text_color="#1C1C1C"
ohos:top_padding="8vp"
ohos:bottom_padding="8vp"
ohos:right_padding="70vp"
ohos:left_padding="70vp"
ohos:center_in_parent="true"
ohos:align_parent_bottom="true"
ohos:bottom_margin="120vp"/>
<Button
ohos:id="$+id:choose_button"
ohos:width="match_content"
ohos:height="match_content"
ohos:text="Choose Image"
ohos:text_size="19fp"
ohos:text_color="#FFFFFF"
ohos:top_padding="8vp"
ohos:bottom_padding="8vp"
ohos:right_padding="70vp"
ohos:left_padding="70vp"
ohos:background_element="$graphic:background_button"
ohos:center_in_parent="true"
ohos:align_parent_bottom="true"
ohos:bottom_margin="75vp"/>
<Button
ohos:id="$+id:button"
ohos:width="match_content"
ohos:height="match_content"
ohos:text="Compress"
ohos:text_size="19fp"
ohos:text_color="#FFFFFF"
ohos:top_padding="8vp"
ohos:bottom_padding="8vp"
ohos:right_padding="70vp"
ohos:left_padding="70vp"
ohos:background_element="$graphic:background_button"
ohos:center_in_parent="true"
ohos:align_parent_bottom="true"
ohos:bottom_margin="15vp"/>
</DependentLayout>

(2)MainAbilitySlice

获取界面的按钮,并分别向按钮绑定点击事件

public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
// 请求文件的读取权限
String[] permissions = {"ohos.permission.READ_USER_STORAGE"};
requestPermissionsFromUser(permissions, 0);
// 获取压缩按钮并绑定事件
Button button = (Button) findComponentById(ResourceTable.Id_button);
if (button != null) {
// 为按钮设置点击回调
button.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
try {
File file = new File(System.getProperty("java.io.tmpdir") + File.separator + tmpName);
HiLog.error(LOG_LABEL, "old size..." + file.length() + " ...b");
// 默认压缩
// File newFile = Compressor.defaultCompress(file);
// 自定义压缩
File newFile = Compressor.customCompress(getContext(), file, 500, 1000, 60);
Text text = (Text) findComponentById(ResourceTable.Id_text);
text.setText("size: " + newFile.length() + " b");
HiLog.error(LOG_LABEL, "new size..." + newFile.length() + " ...b");
PixelMap newPixelMap = Compressor.decode(newFile);
Image image = (Image) findComponentById(ResourceTable.Id_image1);
image.setPixelMap(newPixelMap);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
// 获取选择图片按钮并绑定事件
Button chooseButton = (Button) findComponentById(ResourceTable.Id_choose_button);
if (chooseButton != null) {
// 为按钮设置点击回调
chooseButton.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
DataAbilityHelper helper = DataAbilityHelper.creator(getContext());
try {
ResultSet resultSet = helper.query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,
null, null);
while (resultSet != null && resultSet.goToNextRow()) {
// 互殴媒体库的图片
int id =
resultSet.getInt(resultSet.getColumnIndexForName(AVStorage.Images.Media.ID));
HiLog.error(LOG_LABEL, "id:..." + id + " ...");
Uri uri =
Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, "" + id);
// 根据图片的 uri 打开文件并保存到临时目录中
FileDescriptor fileDescriptor = helper.openFile(uri, "r");
ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions();
decodingOpts.sampleSize = ImageSource.DecodingOptions.DEFAULT_SAMPLE_SIZE;
ImageSource imageSource = ImageSource.create(fileDescriptor, null);
PixelMap pixelMap = imageSource.createThumbnailPixelmap(decodingOpts, true);
ImagePacker imagePacker = ImagePacker.create();
tmpName = UUID.randomUUID().toString();
File file = new File(System.getProperty("java.io.tmpdir") + File.separator +
tmpName);
FileOutputStream outputStream = new FileOutputStream(file);
ImagePacker.PackingOptions packingOptions = new ImagePacker.PackingOptions();
packingOptions.quality = 100;
boolean result = imagePacker.initializePacking(outputStream, packingOptions);
result = imagePacker.addImage(pixelMap);
long dataSize = imagePacker.finalizePacking();
// 显示图片和图片大小
Text text = (Text) findComponentById(ResourceTable.Id_text);
text.setText("size: " + file.length() + " b");
Image image = (Image) findComponentById(ResourceTable.Id_image1);
image.setPixelMap(pixelMap);
}
} catch (DataAbilityRemoteException | FileNotFoundException e) {
e.printStackTrace();
}
}
});
}
}

(3)结果展示


(4)Compressor 核心方法

defaultCompress(Context context, File file)

输入:用户需要处理的文件

输出:处理后的临时文件

处理流程:传入图片后,先将图片拷贝到临时目录,再按照默认的压缩处理方法,完成后返回处理后图片的临时目录

customCompress(Context context, File file, int width, int height, int quality)

输入:用户需要处理的文件,处理后的宽度、高度以及图片质量。

输出:处理后的临时文件

处理流程:传入图片后,先将图片拷贝到临时目录,再按照指定的处理方法进行

压缩,完成后返回处理后图片的临时目录

decode(File file)

输入:图片的目录

输出:图片的像素矩阵形式

作用:用于界面图片展示

写在最后

  • 如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
  • 点赞,转发,有你们的 『点赞和评论』,才是我创造的动力。
  • 关注小编,同时可以期待后续文章ing🚀,不定期分享原创知识。
  • 想要获取更多完整鸿蒙最新学习知识点,请移步前往小编:https://gitee.com/MNxiaona/733GH/blob/master/jianshu

相关文章

网友评论

    本文标题:基于 OpenHarmony compress 三方件

    本文链接:https://www.haomeiwen.com/subject/ezoatjtx.html