saving images and abstract product view

This commit is contained in:
leca 2024-09-17 02:20:27 +03:00
parent 50da2955e8
commit 8496a6f2f4
18 changed files with 306 additions and 57 deletions

View File

@ -0,0 +1,5 @@
package org.foxarmy.barcodescannerforemployees
class AbstractProduct constructor(name:String, netWeight: Double, imageFile: String, type: Int) {
}

View File

@ -0,0 +1,176 @@
package org.foxarmy.barcodescannerforemployees
import android.content.Context
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
/**
* TODO: document your custom view class.
*/
class AbstractProductView: LinearLayout {
private val productImage = ImageView(context)
private val productNameField = TextView(context)
private val netWeightField = TextView(context)
private val categoryField = TextView(context)
constructor(context: Context, productImageFile: String, productName: String, netWeight: Double, category: Int) : super(context) {
// this.layerType =
// setLayerType()
this.orientation = LinearLayout.VERTICAL
// productImage.set
productNameField.setText(productName)
netWeightField.setText(netWeight.toString())
addView(productNameField)
addView(netWeightField)
// if(findViewById(R.id.productLayout) == null) {
// Toast.makeText(context, "AAAAAAAAAAA", Toast.LENGTH_LONG).show()
// }
// findViewById<ConstraintLayout>(R.id.productLayout).addView(productNameField)
// (requestLayout() as RelativeLayout).addView(productNameField)
}
}
//class AbstractProductView : View {
//
// private var _exampleString: String? = null // TODO: use a default from R.string...
// private var _exampleColor: Int = Color.RED // TODO: use a default from R.color...
// private var _exampleDimension: Float = 0f // TODO: use a default from R.dimen...
//
// private lateinit var textPaint: TextPaint
// private var textWidth: Float = 0f
// private var textHeight: Float = 0f
//
// /**
// * The text to draw
// */
// var exampleString: String?
// get() = _exampleString
// set(value) {
// _exampleString = value
// invalidateTextPaintAndMeasurements()
// }
//
// /**
// * The font color
// */
// var exampleColor: Int
// get() = _exampleColor
// set(value) {
// _exampleColor = value
// invalidateTextPaintAndMeasurements()
// }
//
// /**
// * In the example view, this dimension is the font size.
// */
// var exampleDimension: Float
// get() = _exampleDimension
// set(value) {
// _exampleDimension = value
// invalidateTextPaintAndMeasurements()
// }
//
// /**
// * In the example view, this drawable is drawn above the text.
// */
// var exampleDrawable: Drawable? = null
//
// constructor(context: Context) : super(context) {
// init(null, 0)
// }
//
// constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
// init(attrs, 0)
// }
//
// constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
// init(attrs, defStyle)
// }
//
// private fun init(attrs: AttributeSet?, defStyle: Int) {
// // Load attributes
// val a = context.obtainStyledAttributes(
// attrs, R.styleable.AbstractProductView, defStyle, 0
// )
//
// _exampleString = a.getString(
// R.styleable.AbstractProductView_exampleString
// )
// _exampleColor = a.getColor(
// R.styleable.AbstractProductView_exampleColor,
// exampleColor
// )
// // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with
// // values that should fall on pixel boundaries.
// _exampleDimension = a.getDimension(
// R.styleable.AbstractProductView_exampleDimension,
// exampleDimension
// )
//
// if (a.hasValue(R.styleable.AbstractProductView_exampleDrawable)) {
// exampleDrawable = a.getDrawable(
// R.styleable.AbstractProductView_exampleDrawable
// )
// exampleDrawable?.callback = this
// }
//
// a.recycle()
//
// // Set up a default TextPaint object
// textPaint = TextPaint().apply {
// flags = Paint.ANTI_ALIAS_FLAG
// textAlign = Paint.Align.LEFT
// }
//
// // Update TextPaint and text measurements from attributes
// invalidateTextPaintAndMeasurements()
// }
//
// private fun invalidateTextPaintAndMeasurements() {
// textPaint.let {
// it.textSize = exampleDimension
// it.color = exampleColor
// textWidth = it.measureText(exampleString)
// textHeight = it.fontMetrics.bottom
// }
// }
//
// override fun onDraw(canvas: Canvas) {
// super.onDraw(canvas)
//
// // TODO: consider storing these as member variables to reduce
// // allocations per draw cycle.
// val paddingLeft = paddingLeft
// val paddingTop = paddingTop
// val paddingRight = paddingRight
// val paddingBottom = paddingBottom
//
// val contentWidth = width - paddingLeft - paddingRight
// val contentHeight = height - paddingTop - paddingBottom
//
// exampleString?.let {
// // Draw the text.
// canvas.drawText(
// it,
// paddingLeft + (contentWidth - textWidth) / 2,
// paddingTop + (contentHeight + textHeight) / 2,
// textPaint
// )
// }
//
// // Draw the example drawable on top of the text.
// exampleDrawable?.let {
// it.setBounds(
// paddingLeft, paddingTop,
// paddingLeft + contentWidth, paddingTop + contentHeight
// )
// it.draw(canvas)
// }
// }
//}

View File

@ -16,12 +16,23 @@ import com.google.mlkit.vision.barcode.common.Barcode
import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions
import com.google.mlkit.vision.codescanner.GmsBarcodeScanning
import org.foxarmy.barcodescannerforemployees.databinding.AddProductFragmentBinding
import java.io.File
import java.io.FileOutputStream
import java.nio.ByteBuffer
import java.security.MessageDigest
@OptIn(ExperimentalStdlibApi::class)
fun String.md5(): String {
val md = MessageDigest.getInstance("MD5")
val digest = md.digest(this.toByteArray())
return digest.toHexString()
}
/**
* A simple [Fragment] subclass as the default destination in the navigation.
*/
class AddProductFragment : Fragment() {
private var _binding: AddProductFragmentBinding? = null
@ -45,7 +56,18 @@ class AddProductFragment : Fragment() {
when (requestCode) {
200 -> {
if (resultCode == Activity.RESULT_OK && data != null) {
binding.imageView.setImageBitmap(data.extras?.get("data") as Bitmap)
val picture = (data.extras?.get("data") as Bitmap)
binding.imageView.setImageBitmap(picture)
val picturesPath = context?.filesDir
val dir = File(picturesPath, "pictures")
dir.mkdirs()
val filename = picture.toString().md5()
val file = File(dir, filename)
FileOutputStream(file).use {
val b = ByteBuffer.allocate(picture.byteCount)
picture.copyPixelsToBuffer(b)
it.write(b.array())
}
}
}
}
@ -69,6 +91,10 @@ class AddProductFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.saveButton.setOnClickListener {
}
binding.takePictureButton.setOnClickListener {
requestPermissionLauncher.launch(android.Manifest.permission.CAMERA)
}

View File

@ -1,7 +1,6 @@
package org.foxarmy.barcodescannerforemployees
import android.os.Bundle
import android.provider.BaseColumns
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
@ -30,22 +29,22 @@ class MainActivity : AppCompatActivity() {
//
// val netRowId = db?.insert(ProductContract.ProductEntry.TABLE_NAME, null, values)
val db2 = dbHelper.readableDatabase
val projection = arrayOf(BaseColumns._ID, ProductContract.ProductEntry.PRODUCT_NAME, ProductContract.ProductEntry.PRODUCT_NET_WEIGHT, ProductContract.ProductEntry.IMAGE_FILENAME)
val selection = "${ProductContract.ProductEntry.PRODUCT_NAME} = ?"
val selectionArgs = arrayOf("Имя продукта")
val cursor = db2.query(
ProductContract.ProductEntry.TABLE_NAME,
null,
selection,
selectionArgs,
null,
null,
null
)
// val db2 = dbHelper.readableDatabase
//
// val projection = arrayOf(BaseColumns._ID, ProductContract.ProductEntry.PRODUCT_NAME, ProductContract.ProductEntry.PRODUCT_NET_WEIGHT, ProductContract.ProductEntry.IMAGE_FILENAME)
//
// val selection = "${ProductContract.ProductEntry.PRODUCT_NAME} = ?"
// val selectionArgs = arrayOf("Имя продукта")
//
// val cursor = db2.query(
// ProductContract.ProductEntry.TABLE_NAME,
// null,
// selection,
// selectionArgs,
// null,
// null,
// null
// )
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

View File

@ -1,11 +1,10 @@
package org.foxarmy.barcodescannerforemployees
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import androidx.fragment.app.Fragment
import org.foxarmy.barcodescannerforemployees.databinding.FragmentSecondBinding
/**
@ -23,7 +22,6 @@ class MainScreenFragment : Fragment() {
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentSecondBinding.inflate(inflater, container, false)
return binding.root
@ -32,9 +30,9 @@ class MainScreenFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.buttonSecond.setOnClickListener {
findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
}
// binding..setOnClickListener {
// findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
// }
}
override fun onDestroyView() {

View File

@ -5,7 +5,7 @@ class Parser constructor(payloadStartRegex: String, payloadEndRegex: String, pay
val payloadEndRegex: String = payloadEndRegex
val payloadRegex: String = payloadRegex
fun parse(text: String): MutableList<Product> {
fun parse(text: String): MutableList<AbstractProduct> {
val payloadStart = Regex(payloadStartRegex)
val payloadEnd = Regex(payloadEndRegex)

View File

@ -1,7 +0,0 @@
package org.foxarmy.barcodescannerforemployees
class Product constructor(name:String, netWeight: Double) {
var name: String = name
var netWeight: Double = netWeight
var imageFile: String = ""
}

View File

@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
/**
@ -24,4 +25,18 @@ class StorageFragment : Fragment() {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_storage, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
getView()?.findViewById<ConstraintLayout>(R.id.storageLayout)?.addView(
AbstractProductView(
requireContext(),
productImageFile = "test1",
productName = "test2",
netWeight = 1.1,
category = 1
)
)
}
}

View File

@ -8,6 +8,11 @@
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/storageLayout">
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"

View File

@ -4,7 +4,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
app:layout_behavior="@string/appbar_scrolling_view_behavior" android:id="@+id/storageLayout">
<fragment
android:id="@+id/nav_host_fragment_content_main"

View File

@ -9,26 +9,5 @@
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<Button
android:id="@+id/button_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/previous"
app:layout_constraintBottom_toTopOf="@id/textview_second"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/textview_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/product_name_label"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/button_second"/>
</androidx.constraintlayout.widget.ConstraintLayout>
android:padding="16dp"/>
</androidx.core.widget.NestedScrollView>

View File

@ -6,7 +6,7 @@
tools:context=".StorageFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent" android:id="@+id/storageLayout">
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

View File

@ -0,0 +1,26 @@
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="300dp"
android:layout_height="300dp">
<org.foxarmy.barcodescannerforemployees.AbstractProductView
style="@style/Widget.Theme.BarcodeScannerForEmployees.MyView"
android:layout_width="300dp"
android:layout_height="300dp"
android:paddingLeft="20dp"
android:paddingBottom="40dp"
app:exampleDimension="24sp"
app:exampleString="Hello, AbstractProductView"
app:exampleDrawable="@android:drawable/ic_menu_add"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="300dp"
android:layout_height="300dp" android:id="@+id/productLayout">
<LinearLayout
android:orientation="vertical"
android:layout_width="298dp"
android:layout_height="298dp" tools:layout_editor_absoluteY="1dp" tools:layout_editor_absoluteX="1dp">
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

View File

@ -0,0 +1,6 @@
<resources>
<style name="Widget.Theme.BarcodeScannerForEmployees.MyView" parent="">
<item name="android:background">@color/gray_600</item>
<item name="exampleColor">@color/light_blue_600</item>
</style>
</resources>

View File

@ -0,0 +1,8 @@
<resources>
<declare-styleable name="AbstractProductView">
<attr name="exampleString" format="string"/>
<attr name="exampleDimension" format="dimension"/>
<attr name="exampleColor" format="color"/>
<attr name="exampleDrawable" format="color|reference"/>
</declare-styleable>
</resources>

View File

@ -2,4 +2,8 @@
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="light_blue_400">#FF29B6F6</color>
<color name="light_blue_600">#FF039BE5</color>
<color name="gray_400">#FFBDBDBD</color>
<color name="gray_600">#FF757575</color>
</resources>

View File

@ -12,4 +12,7 @@
<string name="takePicture">Take picture</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="productName">Product name</string>
<string name="productType">Product type</string>
<string name="imageOfAProduct">Image of a product</string>
</resources>

View File

@ -0,0 +1,6 @@
<resources>
<style name="Widget.Theme.BarcodeScannerForEmployees.MyView" parent="">
<item name="android:background">@color/gray_400</item>
<item name="exampleColor">@color/light_blue_400</item>
</style>
</resources>