Skip to content

Commit

Permalink
Added Image preview and delete image functionality, UI improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tejas-67 committed May 31, 2023
1 parent ae0f7d5 commit 10220de
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 361 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
android:requestLegacyExternalStorage="true"
>
<activity
android:name=".MainActivity"
android:name=".Activities.MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package com.example.pdf_generator
package com.example.pdf_generator.Activities


import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupActionBarWithNavController
import com.example.pdf_generator.Fragments.CameraFragment
import com.example.pdf_generator.R
import com.example.pdf_generator.UI.AppViewModel
import com.example.pdf_generator.UI.AppViewModelProviderFactory
import com.example.pdf_generator.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var navController: NavController
lateinit var viewModel: AppViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val viewModelProviderFactory=AppViewModelProviderFactory()
viewModel=ViewModelProvider(this, viewModelProviderFactory)
.get(AppViewModel::class.java)

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
supportActionBar?.hide()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.pdf_generator.Activities.MainActivity
import com.example.pdf_generator.UI.AppViewModel
import com.example.pdf_generator.databinding.FragmentCameraBinding
import java.io.ByteArrayOutputStream
import java.io.File
Expand All @@ -39,9 +41,7 @@ class CameraFragment : Fragment()
{
private var _binding:FragmentCameraBinding?=null
private val binding get()=_binding!!

private lateinit var bitmapList:ArrayList<Bitmap>
private var imgList:ArrayList<Uri>?=null
private lateinit var viewModel: AppViewModel
private var cameraManager:CameraManager?=null
private var imgCapture:ImageCapture?=null
private lateinit var getcameraID:String
Expand All @@ -64,51 +64,34 @@ class CameraFragment : Fragment()
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
viewModel=(activity as MainActivity).viewModel
_binding= FragmentCameraBinding.inflate(inflater,container,false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{
super.onViewCreated(view, savedInstanceState)

bitmapList=ArrayList()
imgList= ArrayList()
if (checkallPermission()) startCamera()
else requestPermission()
binding.captureBtn.setOnClickListener{ captureImage() }

binding.saveBtn.setOnClickListener { createPdf(bitmapList, "sample ${randomNumberGeneratorForTest()}")}
binding.homeBtn.setOnClickListener {
val action=CameraFragmentDirections.actionCameraFragmentToHomeFragment()
findNavController().navigate(action)
}
binding.saveBtn.setOnClickListener {
if (imgList!!.isEmpty()) {
Toast.makeText(requireContext(), "click some images", Toast.LENGTH_SHORT).show()
} else {
// val bundle = Bundle()
// bundle.putParcelableArrayList("uri", imgList)
//
// val receiverFragment = ImagePreviewFragment()
// receiverFragment.apply { arguments=bundle }
val imguri:Array<Uri> = imgList!!.toTypedArray()
Toast.makeText(requireContext(), "${imguri.size}", Toast.LENGTH_SHORT).show()
val action = CameraFragmentDirections.actionCameraFragmentToImagePreviewFragment(imguri)
if(viewModel.getListSize()==0) Toast.makeText(requireContext(), "Cannot create empty Pdf", Toast.LENGTH_SHORT).show()
else {
val action=CameraFragmentDirections.actionCameraFragmentToImagePreviewFragment()
findNavController().navigate(action)
}

}


}

// private fun setclickedImageinRV() {
// val adapter=ClickedImagePreviewAdapter(imgList)
// binding.clickedImgRecyclerView.layoutManager= LinearLayoutManager(requireContext(),
// RecyclerView.HORIZONTAL,false)
// binding.clickedImgRecyclerView.adapter=adapter
// }

private
fun checkallPermission() = REQUIRED_PERMISSIONS.all{
ContextCompat.checkSelfPermission(requireContext(), it) == PackageManager.PERMISSION_GRANTED
Expand Down Expand Up @@ -182,50 +165,7 @@ class CameraFragment : Fragment()

fun randomNumberGeneratorForTest(): Int = kotlin.random.Random.nextInt()

private fun scaleBitmapToFitScreenWidth(bitmap: Bitmap, screenWidth: Int): Bitmap {
val bitmapWidth = bitmap.width
val bitmapHeight = bitmap.height
val scaledHeight = (screenWidth.toFloat() / bitmapWidth * bitmapHeight).toInt()
val processedBitmap = Bitmap.createScaledBitmap(bitmap, screenWidth, scaledHeight, true)
val stream = ByteArrayOutputStream()
processedBitmap.compress(Bitmap.CompressFormat.WEBP, 50 ,stream)
val byteArray=stream.toByteArray()
return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
}

fun createPdf(bitmaps: List<Bitmap>, pdfFileName: String) {
if(bitmaps.size==0) {
Toast.makeText(requireContext(), "Click some images to proceed", Toast.LENGTH_SHORT).show()
return
}
val pdfDocument = PdfDocument()

val displayMetrics = Resources.getSystem().displayMetrics
val screenWidth = displayMetrics.widthPixels
for (bitmap in bitmaps) {
val scaledBitmap = scaleBitmapToFitScreenWidth(bitmap, screenWidth)
val pageInfo = PdfDocument.PageInfo.Builder(scaledBitmap.width, scaledBitmap.height, pdfDocument.pages.size + 1).create()
val page = pdfDocument.startPage(pageInfo)
page.canvas.drawBitmap(scaledBitmap, 0f, 0f, null)
pdfDocument.finishPage(page)
}
val directory = Environment.getExternalStoragePublicDirectory("PdfGeneratorDocuments")
if (!directory.exists()) {directory.mkdirs()}

val pdfFilePath = "${directory.path}/$pdfFileName.pdf"
val pdfFile = File(pdfFilePath)

try {
pdfDocument.writeTo(FileOutputStream(pdfFile))
pdfDocument.close()
Toast.makeText(requireContext(), "${pdfFileName}.pdf saved successfully", Toast.LENGTH_SHORT).show()
} catch (e: IOException) {
Log.w(TAG, "Error while creating Pdf: ${e}")
Toast.makeText(requireContext(), "Failed to create PDF", Toast.LENGTH_SHORT).show()
}


}

private fun captureImage() {
var imgCapture = imgCapture ?: return
Expand All @@ -251,16 +191,13 @@ class CameraFragment : Fragment()
val msg = "Photo capture succeeded: ${output.savedUri}"
Log.d(TAG, msg)
val savedUri = output.savedUri
binding.ClickedImageIv.setImageURI(savedUri)
binding.ClickedImage.visibility=View.VISIBLE
savedUri?.let{
imgList?.add(it)
}
if(imgList?.size!=0)binding.saveBtn.visibility=View.VISIBLE

val capturedBitmap = savedUri?.let { getBitmapFromUri(it) }
capturedBitmap?.let { it->
bitmapList.add(it)
binding.ClickedImageIv.setImageBitmap(it)
binding.ClickedImage.visibility=View.VISIBLE
binding.saveBtn.visibility=View.VISIBLE
viewModel.addElementToList(it)
}
}
override fun onError(e: ImageCaptureException){
Expand All @@ -285,14 +222,4 @@ class CameraFragment : Fragment()
null
}
}

override fun onResume() {
super.onResume()
(activity as AppCompatActivity?)!!.supportActionBar!!.hide()
}

override fun onStop() {
super.onStop()
(activity as AppCompatActivity?)!!.supportActionBar!!.show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,4 @@ class HomeFragment : Fragment() {
null
}
}
override fun onResume() {
super.onResume()
(activity as AppCompatActivity?)!!.supportActionBar!!.hide()
}

override fun onStop() {
super.onStop()
(activity as AppCompatActivity?)!!.supportActionBar!!.show()
}
}
Loading

0 comments on commit 10220de

Please sign in to comment.