基本手写应用问题(不是识别)

huangapple go评论86阅读模式
英文:

basic handwriting app issue (not recognition)

问题

以下是您要翻译的部分:

  1. trying to toy around with android studio for a bit, had some inspiration. i tried looking up online and tried ChatGPT but i cant get this to work - what i'm trying to do for now is to show basic handwriting on the screen. the screen is purple colored intentionally so i can see the area im allowed to write in.
  2. im new to this so i cant be too sure about any issues yet. would love if someone could point me in the right direction!
  3. MainActivity.kt:
  4. package com.example.myapplication
  5. import HandWritingView.HandwritingView
  6. import android.content.ContentValues
  7. import android.graphics.Bitmap
  8. import android.os.Bundle
  9. import android.os Environment
  10. import android provider MediaStore
  11. import android.widget Button
  12. import android.widget Toast
  13. import androidx.appcompat.app.AppCompatActivity
  14. class MainActivity : AppCompatActivity() {
  15. private var handwritingView: HandwritingView? = null
  16. override fun onCreate(savedInstanceState: Bundle?) {
  17. super.onCreate(savedInstanceState)
  18. setContentView(R.layout.activity_main)
  19. handwritingView = findViewById<HandwritingView>(R.id.handwriting_view)
  20. val saveButton = findViewById<Button>(R.id.save_button)
  21. saveButton.setOnClickListener {
  22. val bitmap = handwritingView?.getBitmap()
  23. // Save the bitmap as a file in the device's external storage
  24. val contentValues = ContentValues().apply {
  25. put(MediaStore.Images.Media.DISPLAY_NAME, "handwriting_${System.currentTimeMillis()}.jpg")
  26. put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
  27. put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
  28. }
  29. val resolver = applicationContext contentResolver
  30. val uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
  31. uri?.let {
  32. resolver openOutputStream(uri).use { outputStream ->
  33. if (bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, outputStream) == false) {
  34. Toast.makeText(this, "Failed to save image", Toast.LENGTH_SHORT).show()
  35. }
  36. }
  37. Toast.makeText(this, "Image saved successfully", Toast.LENGTH_SHORT).show()
  38. }
  39. }
  40. }
  41. }
  42. activity_main.xml:
  43. <?xml version="1.0" encoding="utf-8"?>
  44. <androidx.constraintlayout.widget.ConstraintLayout
  45. xmlns:android="http://schemas.android.com/apk/res/android"
  46. xmlns:app="http://schemas.android.com/apk/res-auto"
  47. xmlns:tools="http://schemas.android.com/tools"
  48. android:layout_width="match_parent"
  49. android:layout_height="match_parent"
  50. tools:context=".MainActivity">
  51. <TextView
  52. android:id="@+id/textView"
  53. android:layout_width="wrap_content"
  54. android:layout_height="wrap_content"
  55. android:text="Handwrite something and click the 'Save' button to save the image to your device's gallery."
  56. app:layout_constraintBottom_toTopOf="@id/save_button"
  57. app:layout_constraintEnd_toEndOf="parent"
  58. app:layout_constraintStart_toStartOf="parent"
  59. app:layout_constraintTop_toTopOf="parent" />
  60. <HandWritingView.HandwritingView
  61. android:id="@+id/handwriting_view"
  62. android:layout_width="match_parent"
  63. android:layout_height="0dp"
  64. android:background="@color/purple_700"
  65. app:layout_constraintBottom_toTopOf="@id/save_button"
  66. app:layout_constraintTop_toTopOf="parent"
  67. app:layout_constraintVertical_bias="0.0" />
  68. <Button
  69. android:id="@+id/save_button"
  70. android:layout_width="wrap_content"
  71. android:layout_height="wrap_content"
  72. android:layout_marginBottom="32dp"
  73. android:text="Save"
  74. app:layout_constraintBottom_toBottomOf="parent"
  75. app:layout_constraintEnd_toEndOf="parent"
  76. app:layout_constraintHorizontal_bias="0.5"
  77. app:layout_constraintStart_toStartOf="parent" />
  78. </androidx.constraintlayout.widget.ConstraintLayout>
  79. HandwritingView.kt:
  80. package HandWritingView
  81. import android.content Context
  82. import android.graphics.*
  83. import android.util AttributeSet
  84. import android.view MotionEvent
  85. import android.view View
  86. class HandwritingView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
  87. private val paint = Paint().apply {
  88. color = Color.BLACK
  89. style = Paint.Style.STROKE
  90. strokeWidth = 12f
  91. isAntiAlias = true
  92. isDither = true
  93. }
  94. private var path = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
  95. private var canvas = Canvas(path)
  96. private var lastX = 0f
  97. private var lastY = 0f
  98. override fun onDraw(canvas: Canvas) {
  99. super.onDraw(canvas)
  100. canvas drawBitmap(path, null, RectF(0f, 0f, width.toFloat(), height.toFloat()), null)
  101. }
  102. override fun onTouchEvent(event: MotionEvent): Boolean {
  103. val x = event.x
  104. val y = event.y
  105. when (event.action) {
  106. MotionEvent.ACTION_DOWN -> {
  107. lastX = x
  108. lastY = y
  109. performClick() // Call performClick on ACTION_DOWN event
  110. return true
  111. }
  112. MotionEvent.ACTION_MOVE -> {
  113. canvas.drawLine(lastX, lastY, x, y, paint)
  114. lastX = x
  115. lastY = y
  116. invalidate()
  117. return true
  118. }
  119. MotionEvent.ACTION_UP -> {
  120. canvas.drawLine(lastX, lastY, x, y, paint)
  121. invalidate()
  122. return true
  123. }
  124. }
  125. return super.onTouchEvent(event)
  126. }
  127. override fun performClick(): Boolean {
  128. super.performClick()
  129. // Your code here, if any
  130. return true
  131. }
  132. fun clear() {
  133. path = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
  134. canvas = Canvas(path)
  135. invalidate()
  136. }
  137. fun getBitmap(): Bitmap {
  138. return path
  139. }
  140. }

希望这能帮助您解决问题。如果您有任何其他问题,请随时提问。

英文:

trying to toy around with android studio for a bit, had some inspiration. i tried looking up online and tried ChatGPT but i cant get this to work - what i'm trying to do for now is to show basic handwriting on the screen. the screen is purple colored intentionally so i can see the area im allowed to write in.

im new to this so i cant be too sure about any issues yet. would love if someone could point me in the right direction!

MainActivity.kt:

  1. package com.example.myapplication
  2. import HandWritingView.HandwritingView
  3. import android.content.ContentValues
  4. import android.graphics.Bitmap
  5. import android.os.Bundle
  6. import android.os.Environment
  7. import android.provider.MediaStore
  8. import android.widget.Button
  9. import android.widget.Toast
  10. import androidx.appcompat.app.AppCompatActivity
  11. class MainActivity : AppCompatActivity() {
  12. private var handwritingView: HandwritingView? = null
  13. override fun onCreate(savedInstanceState: Bundle?) {
  14. super.onCreate(savedInstanceState)
  15. setContentView(R.layout.activity_main)
  16. handwritingView = findViewById<HandwritingView>(R.id.handwriting_view)
  17. val saveButton = findViewById<Button>(R.id.save_button)
  18. saveButton.setOnClickListener {
  19. val bitmap = handwritingView?.getBitmap()
  20. // Save the bitmap as a file in the device's external storage
  21. val contentValues = ContentValues().apply {
  22. put(MediaStore.Images.Media.DISPLAY_NAME, "handwriting_${System.currentTimeMillis()}.jpg")
  23. put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
  24. put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
  25. }
  26. val resolver = applicationContext.contentResolver
  27. val uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
  28. uri?.let {
  29. resolver.openOutputStream(uri).use { outputStream ->
  30. if (bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, outputStream) == false) {
  31. Toast.makeText(this, "Failed to save image", Toast.LENGTH_SHORT).show()
  32. }
  33. }
  34. Toast.makeText(this, "Image saved successfully", Toast.LENGTH_SHORT).show()
  35. }
  36. }
  37. }
  38. }

activity_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9. <TextView
  10. android:id="@+id/textView"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="Handwrite something and click the 'Save' button to save the image to your device's gallery."
  14. app:layout_constraintBottom_toTopOf="@id/save_button"
  15. app:layout_constraintEnd_toEndOf="parent"
  16. app:layout_constraintStart_toStartOf="parent"
  17. app:layout_constraintTop_toTopOf="parent" />
  18. <HandWritingView.HandwritingView
  19. android:id="@+id/handwriting_view"
  20. android:layout_width="match_parent"
  21. android:layout_height="0dp"
  22. android:background="@color/purple_700"
  23. app:layout_constraintBottom_toTopOf="@id/save_button"
  24. app:layout_constraintTop_toTopOf="parent"
  25. app:layout_constraintVertical_bias="0.0" />
  26. <Button
  27. android:id="@+id/save_button"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_marginBottom="32dp"
  31. android:text="Save"
  32. app:layout_constraintBottom_toBottomOf="parent"
  33. app:layout_constraintEnd_toEndOf="parent"
  34. app:layout_constraintHorizontal_bias="0.5"
  35. app:layout_constraintStart_toStartOf="parent" />
  36. </androidx.constraintlayout.widget.ConstraintLayout>

HandwritingView.kt:

  1. package HandWritingView
  2. import android.content.Context
  3. import android.graphics.*
  4. import android.util.AttributeSet
  5. import android.view.MotionEvent
  6. import android.view.View
  7. class HandwritingView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
  8. private val paint = Paint().apply {
  9. color = Color.BLACK
  10. style = Paint.Style.STROKE
  11. strokeWidth = 12f
  12. isAntiAlias = true
  13. isDither = true
  14. }
  15. private var path = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
  16. private var canvas = Canvas(path)
  17. private var lastX = 0f
  18. private var lastY = 0f
  19. override fun onDraw(canvas: Canvas) {
  20. super.onDraw(canvas)
  21. canvas.drawBitmap(path, null, RectF(0f, 0f, width.toFloat(), height.toFloat()), null)
  22. }
  23. override fun onTouchEvent(event: MotionEvent): Boolean {
  24. val x = event.x
  25. val y = event.y
  26. when (event.action) {
  27. MotionEvent.ACTION_DOWN -> {
  28. lastX = x
  29. lastY = y
  30. performClick() // Call performClick on ACTION_DOWN event
  31. return true
  32. }
  33. MotionEvent.ACTION_MOVE -> {
  34. canvas.drawLine(lastX, lastY, x, y, paint)
  35. lastX = x
  36. lastY = y
  37. invalidate()
  38. return true
  39. }
  40. MotionEvent.ACTION_UP -> {
  41. canvas.drawLine(lastX, lastY, x, y, paint)
  42. invalidate()
  43. return true
  44. }
  45. }
  46. return super.onTouchEvent(event)
  47. }
  48. override fun performClick(): Boolean {
  49. super.performClick()
  50. // Your code here, if any
  51. return true
  52. }
  53. fun clear() {
  54. path = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
  55. canvas = Canvas(path)
  56. invalidate()
  57. }
  58. fun getBitmap(): Bitmap {
  59. return path
  60. }
  61. }

like i said im new to this topic so please dont be too harsh on me if it's inappropriate in anyway. i was lost trying to learn and thought praticing this way is the best way. really sorry if this is the case!

I'm adding my file layout, just in case:

基本手写应用问题(不是识别)

all i was trying to do is a simple handwriting app and show in black the handwriting.
on the emulator i don't even see the purple area at all.
on my physical device(s) i do see it, but cant see any handwriting done, by finger or pen.
im not sure how come those are different (guessing android version? maybe?) and why the handwriting isn't shown.

any help would be much appreciated!!! ty 基本手写应用问题(不是识别)

答案1

得分: 0

你的视图还算接近,但也不完全正确。使用一个Path对象。使用lineTo将所有移动的触摸事件累积到Path中。在onDraw中绘制这个路径。完全不需要Bitmap。

英文:

Your View isn't too far off, but it isn't quite right either. Use a Path object. Accumulate all the move touch events into the Path using lineTo. Draw the path in onDraw. No need to have a Bitmap at all.

huangapple
  • 本文由 发表于 2023年4月11日 05:00:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定