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

173 lines
5.4 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,
} 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";
@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()}
space={20}
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 11:52:38 +08:00
"黑白化",
async () => {
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-22 17:54:23 +08:00
async () => {
const imageInfo = await iv.current.getImageInfo(context);
const pixels = await iv.current.getImagePixels(context);
const data = new Uint32Array(pixels);
2021-11-23 11:52:38 +08:00
extractGrayValue(data);
binarization(data, 127);
iv.current.imagePixels = {
width: imageInfo.width,
height: imageInfo.height,
pixels: pixels,
};
},
],
[
"OSTU",
async () => {
const imageInfo = await iv.current.getImageInfo(context);
const pixels = await iv.current.getImagePixels(context);
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,
};
},
],
[
"FastBlur",
async () => {
const imageInfo = await iv.current.getImageInfo(context);
const pixels = await iv.current.getImagePixels(context);
const data = new Uint32Array(pixels);
2021-11-22 18:11:04 +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,
};
},
],
[
"高斯模糊",
async () => {
const imageInfo = await iv.current.getImageInfo(context);
const pixels = await iv.current.getImagePixels(context);
const data = new Uint32Array(pixels);
2021-11-23 11:52:38 +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,
};
},
],
[
"透明",
async () => {
const imageInfo = await iv.current.getImageInfo(context);
loge(imageInfo);
const pixels = await iv.current.getImagePixels(context);
loge(pixels.byteLength);
const data = new Uint8Array(pixels);
for (let i = 0; i < data.length - 4; i += 4) {
data[i + 3] = 0xff / 2;
}
iv.current.imagePixels = {
width: imageInfo.width,
height: imageInfo.height,
pixels: pixels,
};
},
],
[
"重置",
async () => {
iv.current.imageUrl = undefined;
iv.current.imageUrl = imageUrl;
},
],
] as [string, () => {}][]
).map((e) => (
<Text
layoutConfig={layoutConfig().just()}
width={100}
height={40}
backgroundColor={colors[1]}
textColor={Color.WHITE}
textSize={20}
onClick={e[1]}
>
{e[0]}
</Text>
))}
</VLayout>
</Scroller>;
}
}