0% found this document useful (0 votes)
8 views5 pages

MPC Practical 4

Uploaded by

Meet Savaliya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

MPC Practical 4

Uploaded by

Meet Savaliya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Marwadi University

Faculty of Technology
Department of Information and Communication Technology
Subject: Mobile and Pervasive Aim: Develop an application for multimedia processing. The app will allow
Computing [01CT0716] users to choose or capture an image and display the image as well as details
of it like size and dimensions..
Practical No: 4 Date: 19-09-2024 Enrollment No: - 92210133009

CODE:-

MainActivity.kt:-

class MainActivity : AppCompatActivity() {

private lateinit var imageView: ImageView


private lateinit var textViewDetails: TextView
private val REQUEST_IMAGE_CAPTURE = 1
private val REQUEST_PICK_IMAGE = 2

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val btnPickImage: Button = findViewById(R.id.btnPickImage)


val btnCaptureImage: Button = findViewById(R.id.btnCaptureImage)
imageView = findViewById(R.id.imageView)
textViewDetails = findViewById(R.id.textViewDetails)

btnPickImage.setOnClickListener {
pickImageFromGallery()
}

btnCaptureImage.setOnClickListener {
captureImage()
}

checkPermissions()
}

private fun checkPermissions() {


if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(this, arrayOf(
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE), 100)

1|Page
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Mobile and Pervasive Aim: Develop an application for multimedia processing. The app will allow
Computing [01CT0716] users to choose or capture an image and display the image as well as details
of it like size and dimensions..
Practical No: 4 Date: 19-09-2024 Enrollment No: - 92210133009

}
}

private fun pickImageFromGallery() {


val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent, REQUEST_PICK_IMAGE)
}

private fun captureImage() {


val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {


super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && data != null) {
when (requestCode) {
REQUEST_IMAGE_CAPTURE -> {
val imageBitmap = data.extras?.get("data") as Bitmap
imageView.setImageBitmap(imageBitmap)
displayImageDetails(imageBitmap)
}
REQUEST_PICK_IMAGE -> {
val imageUri: Uri? = data.data
imageUri?.let {
val imageStream = contentResolver.openInputStream(it)
val imageBitmap = BitmapFactory.decodeStream(imageStream)
imageView.setImageBitmap(imageBitmap)
displayImageDetails(imageBitmap)
}
}
}
}
}

private fun displayImageDetails(bitmap: Bitmap) {


val width = bitmap.width
val height = bitmap.height
val byteCount = bitmap.byteCount / 1024 // Convert bytes to KB

2|Page
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Mobile and Pervasive Aim: Develop an application for multimedia processing. The app will allow
Computing [01CT0716] users to choose or capture an image and display the image as well as details
of it like size and dimensions..
Practical No: 4 Date: 19-09-2024 Enrollment No: - 92210133009

textViewDetails.text = "Dimensions: ${width}x${height}, Size: ${byteCount}KB"


}
}
Main Activity.xml:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btnPickImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Image"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"/>

<Button
android:id="@+id/btnCaptureImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture Image"
app:layout_constraintTop_toBottomOf="@+id/btnPickImage"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="10dp"/>

<ImageView
android:id="@+id/imageView"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@+id/btnCaptureImage"
app:layout_constraintStart_toStartOf="parent"

3|Page
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Mobile and Pervasive Aim: Develop an application for multimedia processing. The app will allow
Computing [01CT0716] users to choose or capture an image and display the image as well as details
of it like size and dimensions..
Practical No: 4 Date: 19-09-2024 Enrollment No: - 92210133009

app:layout_constraintEnd_toEndOf="parent"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/textViewDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Image Details"
app:layout_constraintTop_toBottomOf="@id/imageView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Output:-

4|Page

You might also like