# SimpleCropView
**Repository Path**: HarmonyOS-tpc/SimpleCropView
## Basic Information
- **Project Name**: SimpleCropView
- **Description**: SimpleCropView是适用于鸿蒙的图像裁剪库。它简化了裁剪图像的代码,并提供了易于自定义的UI。
- **Primary Language**: Unknown
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 5
- **Forks**: 1
- **Created**: 2021-02-23
- **Last Updated**: 2025-02-21
## Categories & Tags
**Categories**: harmonyos-image
**Tags**: None
## README
# SimpleCropView
SimpleCropView是适用于鸿蒙的图像裁剪库。它简化了裁剪图像的代码,并提供了易于自定义的UI。
### 概述
* 支持普通裁剪和RxJava裁剪
* 支持裁剪类型FIT_IMAGE、RATIO_4_3、RATIO_3_4、SQUARE、RATIO_16_9、RATIO_9_16、FREE、CIRCLE、 CIRCLE_SQUARE
* 支持JPG格式图片裁剪后输出
* 不支持裁剪后保存PNG格式图片
* 不支持CUSTOM类型裁剪图片
* 不支持setCompressQuality,setCompressFormat
* 不支持scv_background_color和setBackgroundColor,请使用ViewGroup的背景色设置
* 不支持roateImage旋转类型裁剪以及相应的动画设置
### 演示
## 集成
```
方式一:
通过library生成har包,添加har包到libs文件夹内
在entry的gradle内添加如下代码
implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
方式二:
allprojects{
repositories{
mavenCentral()
}
}
implementation 'io.openharmony.tpc.thirdlib:SimpleCropView-library:1.0.1'
```
### 示例
# Basic
加载图片:
cropImageView.load(uri).execute(new LoadCallback() {
@Override
public void onSuccess(PixelMap PixelMap) {
}
@Override
public void onError(Throwable e) {
}
});
裁剪:
cropImageView.crop(uri).execute(new CropCallback() {
@Override
public void onSuccess(PixelMap cropped) {
final String uriPath = "file://"+getContext().getFilesDir()+"/test33.jpg";
cropImageView.save(cropped)
.execute(Uri.parse(uriPath), new SaveCallback() {
@Override
public void onSuccess(Uri uri) {
LogUtil.error("test","裁剪保存成功:"+uri.toString());
}
@Override
public void onError(Throwable e) {
}
});
}
@Override
public void onError(Throwable e) {
}
});
# Rxjava:
String uriPath = "file://"+getContext().getFilesDir()+"/test22.jpg";
Observable observable = Observable.create(emitter -> {
cropImageView.crop(uri)
.executeAsSingle()
.flatMap(new Function>() {
@Override
public SingleSource apply(@io.reactivex.annotations.NonNull PixelMap bitmap)
throws Exception {
cropImageView.getContext().getUITaskDispatcher().asyncDispatch(new Runnable() {
@Override
public void run() {
image1.setPixelMap(bitmap);
image1.setVisibility(Component.VISIBLE);
cropImageView.setVisibility(Component.INVISIBLE);
}
});
return cropImageView.save(bitmap)
.executeAsSingle(Uri.parse(uriPath));
}
})
.doOnSubscribe(new Consumer() {
@Override
public void accept(@io.reactivex.annotations.NonNull Disposable disposable)
throws Exception {
}
})
.doFinally(new Action() {
@Override
public void run() throws Exception {
}
})
.subscribeOn(Schedulers.newThread())
.subscribe(new Consumer() {
@Override
public void accept(@io.reactivex.annotations.NonNull Uri uri11) throws Exception {
File file = new File(uri11.getDecodedPath());
emitter.onComplete();
}
}, new Consumer() {
@Override
public void accept(@io.reactivex.annotations.NonNull Throwable throwable)
throws Exception {
emitter.onComplete();
}
});
});
observable.subscribe(new Consumer() {
@Override
public void accept(Integer integer) throws Exception {
}
});
## XML Attributes
XML sample here.
```xml
```
| XML Attribute
(custom:) | Related Method | Description |
|:---|:---|:---|
| scv_img_src | setImageResource(int resId) | Set source image. |
| scv_crop_mode | setCropMode(CropImageView.CropMode mode) | Set crop mode. |
| scv_overlay_color | setOverlayColor(int overlayColor) | Set image overlay color. |
| scv_frame_color | setFrameColor(int frameColor) | Set the image cropping frame color. |
| scv_handle_color | setHandleColor(int frameColor) | Set the handle color. |
| scv_guide_color | setGuideColor(int frameColor) | Set the guide color. |
| scv_guide_show_mode | setGuideShowMode(CropImageView.ShowMode mode) | Set guideline show mode. |
| scv_handle_show_mode | setHandleShowMode(CropImageView.ShowMode mode) | Set handle show mode. |
| scv_handle_size | setHandleSizeInDp(int handleDp) | Set handle radius in density-independent pixels. |
| scv_touch_padding | setTouchPaddingInDp(int paddingDp) | Set the image cropping frame handle touch padding(touch area) in density-independent pixels. |
| scv_min_frame_size | setMinFrameSizeInDp(int minDp) | Set the image cropping frame minimum size in density-independent pixels. |
| scv_frame_stroke_weight | setFrameStrokeWeightInDp(int weightDp) | Set frame stroke weight in density-independent pixels. |
| scv_guide_stroke_weight | setGuideStrokeWeightInDp(int weightDp) | Set guideline stroke weight in density-independent pixels. |
| scv_crop_enabled | setCropEnabled(boolean enabled) | Set whether to show the image cropping frame. |
| scv_initial_frame_scale | setInitialFrameScale(float initialScale) | Set Set initial scale of the frame.(0.01 ~ 1.0) |
| scv_handle_shadow_enabled | setHandleShadowEnabled(boolean handleShadowEnabled) | Set whether to show handle shadows. |
## License
```
The MIT License (MIT)
Copyright (c) 2015 Issei Aoki
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```