1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| using System; using System.IO; using UnityEngine; using UnityEngine.UI;
public class DrawingBoard : MonoBehaviour { public int textureWidth = 512; public int textureHeight = 512; public Color penColor = Color.black; public Color eraserColor = Color.white; public float penSize = 10f; public float minLength = 1f;
private Texture2D canvasTexture; private RawImage rawImage; private bool isFirstClick = true; private Vector2 curMousePosition; private Vector2 previousMousePosition; private Vector3 finalPosition; private DrawTool curDrawTool = DrawTool.Pen; private enum DrawTool { Pen = 0, Eraser = 1 }
void Start() { rawImage = GetComponent<RawImage>();
CreateCanvasTexture(); }
void Update() { if (Input.GetMouseButton(0)) { curMousePosition = Input.mousePosition; curMousePosition = new Vector2(curMousePosition.x - (Screen.width - textureWidth) / 2, curMousePosition.y - (Screen.height - textureHeight) / 2); if (isFirstClick) { finalPosition = curMousePosition; isFirstClick = false; } else { Vector2 diff = curMousePosition - previousMousePosition; if (diff.magnitude > minLength) { finalPosition = previousMousePosition + diff.normalized * minLength; } else { finalPosition = curMousePosition; } } DrawOnCanvas(finalPosition); previousMousePosition = finalPosition; } if (Input.GetMouseButtonUp(0)) { isFirstClick = true; } }
private void CreateCanvasTexture() { canvasTexture = new Texture2D(textureWidth, textureHeight); rawImage.texture = canvasTexture;
ResetTexture(); }
public void ResetTexture() { Color[] pixels = canvasTexture.GetPixels(); for (int i = 0; i < pixels.Length; i++) { pixels[i] = Color.white; } canvasTexture.SetPixels(pixels); canvasTexture.Apply(); }
private void DrawOnCanvas(Vector2 pos) { int centerX = Mathf.RoundToInt(pos.x); int centerY = Mathf.RoundToInt(pos.y);
if (centerX >= 0 && centerX < canvasTexture.width && centerY >= 0 && centerY < canvasTexture.height) { DrawPixel(centerX, centerY, curDrawTool); } }
private void DrawPixel(int x, int y, DrawTool tool) { Color color = Color.white; switch (tool) { case DrawTool.Pen: color = penColor; break; case DrawTool.Eraser: color = eraserColor; break; }
int radius = Mathf.RoundToInt(penSize) / 2;
for (int i = -radius; i <= radius; i++) { for (int j = -radius; j <= radius; j++) { int pixelX = x + i; int pixelY = y + j;
if (pixelX >= 0 && pixelX < canvasTexture.width && pixelY >= 0 && pixelY < canvasTexture.height) { if (Mathf.Pow(pixelX - x, 2) + Mathf.Pow(pixelY - y, 2) <= Mathf.Pow(radius, 2)) { canvasTexture.SetPixel(pixelX, pixelY, color); } } } }
canvasTexture.Apply(); }
public void SaveCanvasTextureToFile() { byte[] bytes = canvasTexture.EncodeToPNG(); string time = DateTime.Now.ToString(); time = time.Replace("/", "_"); time = time.Replace(" ", "_"); time = time.Replace(":", "_"); string filePath = Application.streamingAssetsPath + "/OCR/Images/ocr_image_" + time + ".png"; File.WriteAllBytes(filePath, bytes); }
private Texture2D WhiteToAlpha0(Texture2D originalTexture) { if (originalTexture == null) return null;
Texture2D newTexture = new Texture2D(originalTexture.width, originalTexture.height);
for (int x = 0; x < originalTexture.width; x++) { for (int y = 0; y < originalTexture.height; y++) { Color pixelColor = originalTexture.GetPixel(x, y);
if (pixelColor.r >= 0.99f && pixelColor.g >= 0.99f && pixelColor.b >= 0.99f) { newTexture.SetPixel(x, y, new Color(0, 0, 0, 0)); } else { newTexture.SetPixel(x, y, pixelColor); } } }
newTexture.Apply();
return newTexture; } }
|