FrostHard

DetectFace.kt

Jul 23rd, 2020
873
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.github.gmarcell.todolist.notification
  2.  
  3. import android.Manifest
  4. import android.annotation.SuppressLint
  5. import android.app.Activity
  6. import android.app.AlertDialog
  7. import android.content.ContentValues
  8. import android.content.Context
  9. import android.content.Intent
  10. import android.content.pm.PackageManager
  11. import android.graphics.*
  12. import android.net.Uri
  13. import android.os.Build
  14. import android.os.Bundle
  15. import android.os.Vibrator
  16. import android.provider.MediaStore
  17. import android.view.View
  18. import android.widget.Button
  19. import android.widget.TextView
  20. import android.widget.Toast
  21. import androidx.appcompat.app.AppCompatActivity
  22. import com.github.gmarcell.todolist.AddEditDoesActivity
  23. import com.github.gmarcell.todolist.MainActivity
  24. import com.github.gmarcell.todolist.R
  25. import com.google.android.gms.vision.Frame
  26. import com.google.android.gms.vision.face.FaceDetector
  27. import kotlinx.android.synthetic.main.activity_take__picture.*
  28.  
  29.  
  30. class DetectFace : AppCompatActivity() {
  31.  
  32.     private val PERMISSION_CODE = 1000
  33.     private val IMAGE_CAPTURE_CODE = 1001
  34.     var image_uri: Uri? = null
  35.  
  36.     @SuppressLint("ObsoleteSdkInt")
  37.     override fun onCreate(savedInstanceState: Bundle?) {
  38.         super.onCreate(savedInstanceState)
  39.         setContentView(R.layout.activity_take__picture)
  40.  
  41.         val vibrate = this.applicationContext.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
  42.  
  43.         val capture_btn = findViewById<Button>(R.id.capture_btn)
  44.         val check_btn = findViewById<Button>(R.id.check_btn)
  45.  
  46.         val titletext = findViewById<TextView>(R.id.Titletext)
  47.         val desctext = findViewById<TextView>(R.id.Desctext)
  48.         val intent =intent.extras
  49.         val title = intent.getString(AddEditDoesActivity.EXTRA_TITLE)
  50.         val desc = intent.getString(AddEditDoesActivity.EXTRA_DESCRIPTION)
  51.         titletext.text = title
  52.         desctext.text = desc
  53.  
  54.         //button click
  55.         capture_btn.setOnClickListener {
  56.             //if system os is Marshmallow or Above, we need to request runtime permission
  57.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  58.                 if (checkSelfPermission(Manifest.permission.CAMERA)
  59.                     == PackageManager.PERMISSION_DENIED ||
  60.                     checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
  61.                     == PackageManager.PERMISSION_DENIED
  62.                 ) {
  63.                     //permission was not enabled
  64.                     val permission = arrayOf(
  65.                         Manifest.permission.CAMERA,
  66.                         Manifest.permission.WRITE_EXTERNAL_STORAGE
  67.                     )
  68.                     //show popup to request permission
  69.                     requestPermissions(permission, PERMISSION_CODE)
  70.                 } else {
  71.                     //permission already granted
  72.                     openCamera()
  73.                 }
  74.             } else {
  75.                 //system os is < marshmallow
  76.                 openCamera()
  77.             }
  78.         }
  79.         check_btn.setOnClickListener (View.OnClickListener{ v ->
  80.             val options = BitmapFactory.Options()
  81.             options.inMutable = true
  82.             val myBitmap = MediaStore.Images.Media.getBitmap(this.contentResolver, image_uri)
  83.             val myRectPaint = Paint()
  84.             myRectPaint.strokeWidth = 10f
  85.             myRectPaint.color = Color.RED
  86.             myRectPaint.style = Paint.Style.STROKE
  87.             val tempBitmap = Bitmap.createBitmap(myBitmap.width, myBitmap.height, Bitmap.Config.RGB_565)
  88.             val tempCanvas = Canvas(tempBitmap)
  89.             tempCanvas.drawBitmap(myBitmap, 0f, 0f, null)
  90.             val faceDetector = FaceDetector.Builder(applicationContext).setTrackingEnabled(false)
  91.                 .build()
  92.             if (!faceDetector.isOperational) {
  93.                 AlertDialog.Builder(v.context).setMessage("Could not set up the face detector!").show()
  94.                 return@OnClickListener
  95.             }
  96.             val frame = Frame.Builder().setBitmap(myBitmap).build()
  97.             val faces = faceDetector.detect(frame)
  98.             val jumlah = faces.size()
  99.             if (jumlah == 0){
  100.                 Toast.makeText(this, "No Face Detected", Toast.LENGTH_SHORT).show()
  101.             }
  102.             else{
  103.                 //Toast.makeText(this, jumlah.toString(), Toast.LENGTH_SHORT).show()
  104.                 Toast.makeText(this, "Nice Face", Toast.LENGTH_SHORT).show()
  105.                 vibrate.cancel()
  106.                 val intentini = Intent(this, MainActivity::class.java)
  107.                 startActivity(intentini)
  108.             }
  109.  
  110. //            for (i in 0 until faces.size()) {
  111. //                val thisFace = faces.valueAt(i)
  112. //                val x1 = thisFace.position.x
  113. //                val y1 = thisFace.position.y
  114. //                val x2 = x1 + thisFace.width
  115. //                val y2 = y1 + thisFace.height
  116. //                tempCanvas.drawRoundRect(RectF(x1, y1, x2, y2), 2f, 2f, myRectPaint)
  117. //            }
  118. //            myImageView.setImageDrawable(BitmapDrawable(resources, tempBitmap))
  119.  
  120. //            val jumlah = faces.size().toString()
  121. //            Toast.makeText(this, jumlah, Toast.LENGTH_SHORT).show()
  122.         })
  123.     }
  124.  
  125.     private fun openCamera() {
  126.         val values = ContentValues()
  127.         values.put(MediaStore.Images.Media.TITLE, "New Picture")
  128.         values.put(MediaStore.Images.Media.DESCRIPTION, "From the Camera")
  129.         image_uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
  130.         //camera intent
  131.         val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
  132.         cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri)
  133.         startActivityForResult(cameraIntent, IMAGE_CAPTURE_CODE)
  134.     }
  135.  
  136.     override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
  137.         //called when user presses ALLOW or DENY from Permission Request Popup
  138.         when(requestCode){
  139.             PERMISSION_CODE -> {
  140.                 if (grantResults.size > 0 && grantResults[0] ==
  141.                     PackageManager.PERMISSION_GRANTED){
  142.                     //permission from popup was granted
  143.                     openCamera()
  144.                 }
  145.                 else{
  146.                     //permission from popup was denied
  147.                     Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show()
  148.                 }
  149.             }
  150.         }
  151.     }
  152.  
  153.     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  154.         //called when image was captured from camera intent
  155.         if (resultCode == Activity.RESULT_OK){
  156.             //set image captured to image view
  157.             image_view.setImageURI(image_uri)
  158.         }
  159.     }
  160. }
RAW Paste Data