参考资源:
基于图像模块实现图库图片的四种常见操作开发分享
效果图
实现布局
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">
<Image
ohos:id="$+id:image"
ohos:height="500vp"
ohos:width="match_parent"
ohos:background_element="#000"
ohos:image_src="$media:zhouye2"
ohos:scale_mode="zoom_center"/>
<DirectionalLayout
ohos:height="match_content"
ohos:width="match_parent"
ohos:orientation="horizontal">
<Text
ohos:height="match_content"
ohos:width="60vp"
ohos:text="Red"
ohos:text_size="20fp"/>
<Slider
ohos:id="$+id:red_value_slider"
ohos:height="match_content"
ohos:width="match_parent"
ohos:layout_alignment="vertical_center"
ohos:left_padding="10vp"
ohos:max="100"
ohos:progress="100"
ohos:progress_color="red"
ohos:right_padding="10vp"/>
</DirectionalLayout>
<DirectionalLayout
ohos:height="match_content"
ohos:width="match_parent"
ohos:orientation="horizontal">
<Text
ohos:height="match_content"
ohos:width="60vp"
ohos:text="Green"
ohos:text_size="20fp"/>
<Slider
ohos:id="$+id:green_value_slider"
ohos:height="match_content"
ohos:width="match_parent"
ohos:layout_alignment="vertical_center"
ohos:left_padding="10vp"
ohos:max="100"
ohos:progress="100"
ohos:progress_color="green"
ohos:right_padding="10vp"/>
</DirectionalLayout>
<DirectionalLayout
ohos:height="match_content"
ohos:width="match_parent"
ohos:orientation="horizontal">
<Text
ohos:height="match_content"
ohos:width="60vp"
ohos:text="Blue"
ohos:text_size="20fp"/>
<Slider
ohos:id="$+id:blue_value_slider"
ohos:height="match_content"
ohos:width="match_parent"
ohos:layout_alignment="vertical_center"
ohos:left_padding="10vp"
ohos:max="100"
ohos:progress="100"
ohos:progress_color="blue"
ohos:right_padding="10vp"/>
</DirectionalLayout>
</DirectionalLayout>
效果图如下:
代码
public class MainAbilitySlice extends AbilitySlice {
private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x01, "TAG");
Image mImage;
PixelMap mRawImagePixelMap;
Slider mRedSlider;
Slider mGreenSlider;
Slider mBlueSlider;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
initComponent();
initData();
initActionListener();
}
private void initData() {
new Thread(new Runnable() {
@Override
public void run() {
PixelMap pixelMapFromResource =
getPixelMapFromResource(ResourceTable.Media_zhouye2);
getUITaskDispatcher().asyncDispatch(new Runnable() {
@Override
public void run() {
mImage.setPixelMap(pixelMapFromResource);
mRawImagePixelMap = pixelMapFromResource;
HiLog.error(LABEL_LOG, "mRawImagePixelMap=" + mRawImagePixelMap);
}
});
}
}).start();
}
private void initActionListener() {
mRedSlider.setValueChangedListener(new Slider.ValueChangedListener() {
@Override
public void onProgressUpdated(Slider slider, int i, boolean b) {
}
@Override
public void onTouchStart(Slider slider) {
}
@Override
public void onTouchEnd(Slider slider) {
PixelMap pixelMap =
changeRGB(getPixelMapFromResource(ResourceTable.Media_zhouye2),
(float) (slider.getProgress() * 1.0 / 100.0),
(float) (mGreenSlider.getProgress() * 1.0 / 100.0),
(float) (mBlueSlider.getProgress() * 1.0 / 100.0));
mImage.setPixelMap(pixelMap);
}
});
mGreenSlider.setValueChangedListener(new Slider.ValueChangedListener() {
@Override
public void onProgressUpdated(Slider slider, int i, boolean b) {
}
@Override
public void onTouchStart(Slider slider) {
}
@Override
public void onTouchEnd(Slider slider) {
PixelMap pixelMap =
changeRGB(getPixelMapFromResource(ResourceTable.Media_zhouye2),
(float) (mRedSlider.getProgress() * 1.0 / 100.0),
(float) (slider.getProgress() * 1.0 / 100.0),
(float) (mBlueSlider.getProgress() * 1.0 / 100.0));
mImage.setPixelMap(pixelMap);
}
});
mBlueSlider.setValueChangedListener(new Slider.ValueChangedListener() {
@Override
public void onProgressUpdated(Slider slider, int i, boolean b) {
}
@Override
public void onTouchStart(Slider slider) {
}
@Override
public void onTouchEnd(Slider slider) {
PixelMap pixelMap =
changeRGB(getPixelMapFromResource(ResourceTable.Media_zhouye2),
(float) (mRedSlider.getProgress() * 1.0 / 100.0),
(float) (mGreenSlider.getProgress() * 1.0 / 100.0),
(float) (slider.getProgress() * 1.0 / 100.0));
mImage.setPixelMap(pixelMap);
}
});
}
// 获取图片资源
private PixelMap getPixelMapFromResource(int resourceId) {
InputStream inputStream = null;
try {
// 创建图像数据源ImageSource对象
inputStream = getContext().getResourceManager().getResource(resourceId);
ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
srcOpts.formatHint = "image/jpg";
ImageSource imageSource = ImageSource.create(inputStream, srcOpts);
// 设置图片参数
ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
decodingOptions.editable = true;
return imageSource.createPixelmap(decodingOptions);
} catch (IOException e) {
HiLog.info(LABEL_LOG, "IOException");
} catch (NotExistException e) {
HiLog.info(LABEL_LOG, "NotExistException");
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
HiLog.info(LABEL_LOG, "inputStream IOException");
}
}
}
return null;
}
// 修改PixMap的RGB值
public static PixelMap changeRGB
(PixelMap rawImagePixelMap, float redRate, float greenRate,
float blueRate) {
HiLog.error(LABEL_LOG, redRate + " " + greenRate + " " + blueRate);
ImageInfo info = rawImagePixelMap.getImageInfo();
int height = info.size.height;
int width = info.size.width;
HiLog.error(LABEL_LOG, "1. toGrayPixMap: " + info.size);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Position position = new Position(i, j);
try {
int rgb = rawImagePixelMap.readPixel(position);
int alpha = rgb & 0xFF000000; // 透明度 取出高八位
int red = (rgb & 0x00FF0000) >> 16;
int green = (rgb & 0x0000FF00) >> 8;
int blue = (rgb & 0x000000FF);
//新的ARGB
int newRgb =
alpha | (((int) (red * redRate)) << 16) | ((((int) (green * greenRate)) << 8) | (((int) (blue * blueRate))));
rawImagePixelMap.writePixel(position, newRgb);
} catch (Exception e) {
HiLog.error(LABEL_LOG, e.getMessage() + " " + i + " " + j);
}
}
}
HiLog.error(LABEL_LOG, "2. toGrayPixMap: " + info.size);
return rawImagePixelMap;
}
private void initComponent() {
mImage = (Image) findComponentById(ResourceTable.Id_image);
mRedSlider = (Slider) findComponentById(ResourceTable.Id_red_value_slider);
mGreenSlider = (Slider) findComponentById(ResourceTable.Id_green_value_slider);
mBlueSlider = (Slider) findComponentById(ResourceTable.Id_blue_value_slider);
}
}