Tooth uncle tutorial is easy to understand

Results show

KMeans data classification

  • The author of KMeans algorithm is MacQueen. KMeans algorithm is an algorithm for data classification, which adopts hard classification method and belongs to unsupervised learning algorithm.
  • For a given sample set, the sample is divided into K clusters according to the distance between samples, so that the points in the cluster are connected together as closely as possible, and the distance between clusters is as large as possible.

Parameter control

  • The number of colors in the cluster, the first image is 3 colors, and the others are 10 colors

Skill points

  • Full screen
  • Use kmeans in autoJS
  • Select the file
  • Paint color card
  • List the use of
  • Browse and save mat
  • Color RGB three channel separation
  • The Toolbar modifies the upper-left icon and its color

The environment

Phone: Mi 11 Pro

Android version: 12

Autojs version: 9.1.6

The code on

1. Color card is a View
<View id="colorCard" marginTop="10" h="40dp" visibility="gone" bg="#ff0000"></View>
Copy the code

Color clustering, after obtaining the data, draw the color card with the following method

function drawColor(view, dataList) { var drawable = new android.graphics.drawable.Drawable({ draw: function (canvas) { var len = dataList.length; let viewWidth = view.getWidth(); let viewHeight = view.getHeight(); let lastX = 0; let paint = new android.graphics.Paint(); for (var i = 0; i < len; i++) { let color = dataList[i].color; let percent = dataList[i].percent; let x = lastX; let y = 0; let width = viewWidth * percent; let height = viewHeight; lastX = x + width; let rect = new android.graphics.Rect(x, y, x + width, y + height); paint.setColor(colors.parseColor(color)); canvas.drawRect(rect, paint); }}}); view.setBackgroundDrawable(drawable); }Copy the code
2. Color list
<list id="list" marginTop="10">
	<horizontal gravity="center" w="*" marginTop="16">
		<card cardCornerRadius="15" w="30" h="30" marginRight="60" bg="#ff00ff">
			<View id="colorDisplayArea" bg="{{this.color}}">
			</View>
		</card>
		<text id="colorValue" w="110dp" marginRight="60" text="{{this.color}}" size="20sp" textColor="# 000000">
		</text>
		<text id="colorRatio" w="60dp" text="{{this.percent}}" size="20sp" textColor="# 000000">
		</text>
	</horizontal>
</list>
Copy the code

To make it nice, let’s make the View a circle, and show the color value, and the color ratio

3. Select a file
ui.logo.click(function () {
  intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  activity.startActivityForResult(intent, imgRequestCode);
});
Copy the code

Before selecting a file, set the file selection listener and get the file path

4. Listen for the file selection result
activity.getEventEmitter().on("activity_result".(requestCode, resultCode, data) = > {
  if (requestCode == imgRequestCode) {
    if (resultCode == activity.RESULT_OK) {
      let uri = data.getData();
      let path = uri.getPath();
      path = decodeURIComponent(path); }}});Copy the code
5. Module debugging
if (typeof module === "object") {
  module.exports = getColorData;
} else {
  let imgPath = "./small11645897022391.png";
  getColorData(imgPath);
}
Copy the code

Module. exports can be used to check whether a module is an object or not without switching the module.

If yes, it is used as a module, if not, it is debugging the module,

This way you don’t have to keep switching between module. Exports comments

6. Picture type
Imgproc.cvtColor(mat, mat2, Imgproc.COLOR_BGRA2RGBA);
Copy the code

Before saving the image, you need to change the color type of the image, because OpencV is a BGR image and AutoJS is an RGB image

Quotes.

Ideas are the most important, other Baidu, Bing, StackOverflow, Github, Android docs, AutoJS docs, and last but not least, ask in the group

The statement

This tutorial is intended for learning purposes only and is not intended for any other use

Wechat official account tooth Uncle tutorial