This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-demo/src/ImageProcessorDemo.tsx

220 lines
7.0 KiB
TypeScript
Raw Normal View History

2021-11-22 17:54:23 +08:00
import {
Group,
Panel,
jsx,
Color,
layoutConfig,
Image,
Scroller,
VLayout,
Text,
Gravity,
createRef,
loge,
2021-11-23 12:13:23 +08:00
ViewComponent,
HLayout,
2021-11-22 17:54:23 +08:00
} from "doric";
2021-11-23 11:52:38 +08:00
import {
binarization,
extractGrayValue,
fastBlur,
gaussianBlur,
ostu,
vampix,
} from "./imageUtils";
2021-11-22 17:54:23 +08:00
import { colors } from "./utils";
2021-11-23 12:13:23 +08:00
@ViewComponent
export class Label extends Text {
constructor() {
super();
this.width = 100;
this.height = 40;
this.backgroundColor = colors[1];
this.textColor = Color.WHITE;
this.textSize = 20;
}
}
2021-11-22 17:54:23 +08:00
@Entry
export class ImageProcessorDemo extends Panel {
build(root: Group): void {
const iv = createRef<Image>();
2021-11-23 11:52:38 +08:00
// const imageUrl = "https://doric.pub/about/The_Parthenon_in_Athens.jpg";
const imageUrl =
"https://img-blog.csdn.net/20181022194542112?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzA0NjY1Mw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70";
//"https://doric.pub/logo.png";
2021-11-22 17:54:23 +08:00
<Scroller parent={root} layoutConfig={layoutConfig().most()}>
<VLayout
layoutConfig={layoutConfig().mostWidth().fitHeight()}
gravity={Gravity.Center}
>
<Text
layoutConfig={layoutConfig().mostWidth().justHeight()}
textSize={30}
textColor={Color.WHITE}
backgroundColor={colors[5]}
textAlignment={Gravity.Center}
height={50}
>
</Text>
<Image
ref={iv}
layoutConfig={layoutConfig().justWidth().fitHeight()}
width={(Environment.screenWidth / 5) * 4}
imageUrl={imageUrl}
/>
2021-11-23 12:13:23 +08:00
<VLayout
layoutConfig={layoutConfig().mostWidth().fitHeight()}
padding={{ left: 10, right: 10, top: 10, bottom: 10 }}
>
<Label
backgroundColor={Color.RED}
layoutConfig={layoutConfig().just()}
onClick={async () => {
iv.current.imageUrl = undefined;
iv.current.imageUrl = imageUrl;
}}
>
</Label>
</VLayout>
<VLayout
layoutConfig={layoutConfig().mostWidth().fitHeight()}
padding={{ left: 10, right: 10, top: 10, bottom: 10 }}
backgroundColor={colors[3].alpha(0.1)}
space={5}
>
<Text textSize={20}></Text>
<HLayout space={10}>
<Label
layoutConfig={layoutConfig().just()}
onClick={async () => {
2021-11-23 11:52:38 +08:00
const imageInfo = await iv.current.getImageInfo(context);
const pixels = await iv.current.getImagePixels(context);
const data = new Uint32Array(pixels);
vampix(data);
iv.current.imagePixels = {
width: imageInfo.width,
height: imageInfo.height,
pixels: pixels,
};
2021-11-23 12:13:23 +08:00
}}
>
</Label>
<Label
layoutConfig={layoutConfig().just()}
onClick={async () => {
2021-11-22 17:54:23 +08:00
const imageInfo = await iv.current.getImageInfo(context);
2021-11-23 12:13:23 +08:00
loge(imageInfo);
2021-11-22 17:54:23 +08:00
const pixels = await iv.current.getImagePixels(context);
2021-11-23 12:13:23 +08:00
loge(pixels.byteLength);
const data = new Uint8Array(pixels);
for (let i = 0; i < data.length - 4; i += 4) {
data[i + 3] = 0xff / 2;
}
2021-11-23 11:52:38 +08:00
iv.current.imagePixels = {
width: imageInfo.width,
height: imageInfo.height,
pixels: pixels,
};
2021-11-23 12:13:23 +08:00
}}
>
</Label>
</HLayout>
</VLayout>
<VLayout
layoutConfig={layoutConfig().mostWidth().fitHeight()}
padding={{ left: 10, right: 10, top: 10, bottom: 10 }}
backgroundColor={colors[2].alpha(0.1)}
space={5}
>
<Text textSize={20}></Text>
<HLayout space={10}>
<Label
layoutConfig={layoutConfig().just()}
onClick={async () => {
2021-11-23 11:52:38 +08:00
const imageInfo = await iv.current.getImageInfo(context);
const pixels = await iv.current.getImagePixels(context);
const data = new Uint32Array(pixels);
2021-11-23 12:13:23 +08:00
fastBlur(data, imageInfo.width, imageInfo.height, 30);
2021-11-22 17:54:23 +08:00
iv.current.imagePixels = {
width: imageInfo.width,
height: imageInfo.height,
pixels: pixels,
};
2021-11-23 12:13:23 +08:00
}}
>
FastBlur
</Label>
<Label
layoutConfig={layoutConfig().just()}
onClick={async () => {
2021-11-22 17:54:23 +08:00
const imageInfo = await iv.current.getImageInfo(context);
const pixels = await iv.current.getImagePixels(context);
const data = new Uint32Array(pixels);
2021-11-23 12:13:23 +08:00
gaussianBlur(data, imageInfo.width, imageInfo.height, 1);
2021-11-22 17:54:23 +08:00
iv.current.imagePixels = {
width: imageInfo.width,
height: imageInfo.height,
pixels: pixels,
};
2021-11-23 12:13:23 +08:00
}}
>
</Label>
</HLayout>
</VLayout>
<VLayout
layoutConfig={layoutConfig().mostWidth().fitHeight()}
padding={{ left: 10, right: 10, top: 10, bottom: 10 }}
backgroundColor={colors[3].alpha(0.1)}
space={5}
>
<Text textSize={20}></Text>
<HLayout space={10}>
<Label
layoutConfig={layoutConfig().just()}
onClick={async () => {
2021-11-22 17:54:23 +08:00
const imageInfo = await iv.current.getImageInfo(context);
const pixels = await iv.current.getImagePixels(context);
const data = new Uint32Array(pixels);
2021-11-23 12:13:23 +08:00
extractGrayValue(data);
binarization(data, 127);
2021-11-22 17:54:23 +08:00
iv.current.imagePixels = {
width: imageInfo.width,
height: imageInfo.height,
pixels: pixels,
};
2021-11-23 12:13:23 +08:00
}}
>
</Label>
<Label
layoutConfig={layoutConfig().just()}
onClick={async () => {
2021-11-22 17:54:23 +08:00
const imageInfo = await iv.current.getImageInfo(context);
const pixels = await iv.current.getImagePixels(context);
2021-11-23 12:13:23 +08:00
const data = new Uint32Array(pixels);
extractGrayValue(data);
const t = ostu(data);
binarization(data, t);
2021-11-22 17:54:23 +08:00
iv.current.imagePixels = {
width: imageInfo.width,
height: imageInfo.height,
pixels: pixels,
};
2021-11-23 12:13:23 +08:00
}}
>
OSTU
</Label>
</HLayout>
</VLayout>
2021-11-22 17:54:23 +08:00
</VLayout>
</Scroller>;
}
}