From a74ccdcfcbd2f09c306d2d3c0f1622d9792e387d Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Wed, 14 Aug 2024 22:18:06 +0930 Subject: [PATCH] Rudimentary thumbnail pixels-based autocrop detection. A better solution would be using element bounds, but that requires a higher API level so I need a working fallback. --- app/build.gradle.kts | 2 +- app/src/main/java/com/lhw/pdf/MainActivity.kt | 51 ++++++------------- .../com/lhw/pdf/ui/gallery/GalleryFragment.kt | 2 +- .../java/com/lhw/pdf/ui/home/HomeFragment.kt | 2 +- .../lhw/pdf/ui/slideshow/SlideshowFragment.kt | 2 +- 5 files changed, 20 insertions(+), 39 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 36cbc0a..66aaaaa 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -9,7 +9,7 @@ android { defaultConfig { applicationId = "com.lhw.pdf" - minSdk = 25 + minSdk = 26 targetSdk = 35 versionCode = 1 versionName = "1.0" diff --git a/app/src/main/java/com/lhw/pdf/MainActivity.kt b/app/src/main/java/com/lhw/pdf/MainActivity.kt index 496f22f..5eaf5f2 100644 --- a/app/src/main/java/com/lhw/pdf/MainActivity.kt +++ b/app/src/main/java/com/lhw/pdf/MainActivity.kt @@ -28,51 +28,33 @@ import androidx.core.view.WindowInsetsCompat import com.lhw.pdf.databinding.ActivityMainBinding import java.io.File import java.io.FileOutputStream +import java.io.InputStream +import kotlin.math.min class MainActivity : AppCompatActivity() { private lateinit var appBarConfiguration: AppBarConfiguration private lateinit var binding: ActivityMainBinding - private lateinit var fileCached: File - private lateinit var renderer: PdfRenderer private val thumbnailWidth = 500 private val thumbnailHeight = 700 - private val bitmapThumbnails = mutableMapOf() -// private val bitmapPagesMain = mutableMapOf() - private val bitmapPagesPresentation = mutableMapOf() - private val testPdf = R.raw.testpdf + private lateinit var pdfDocument: PdfDocument private lateinit var presentationScroll: HorizontalScrollView private lateinit var presentationLayout: LinearLayout - private fun initializeRenderer() { + private fun inputStreamToCache(outputFilename: String, inputStream: InputStream): File { + val fileCached = File(cacheDir, outputFilename) val output = FileOutputStream(fileCached) - val inputStream = resources.openRawResource(testPdf) while (inputStream.available() > 0) { inputStream.copyTo(output) } inputStream.close() output.close() - val pfd = ParcelFileDescriptor.open(fileCached, ParcelFileDescriptor.MODE_READ_ONLY) - renderer = PdfRenderer(pfd) + return fileCached } - private fun renderPagesToMap(map: MutableMap, width: Int, height: Int) { - map.clear() - val numPages = renderer.pageCount - for (i in 0..(R.id.thumbnails_layout) - for (bitmap in bitmapThumbnails.values) { + for (bitmap in pdfDocument.bitmapThumbnails.values) { val img = ImageView(this) img.setImageBitmap(bitmap) container.addView(img) diff --git a/app/src/main/java/com/lhw/pdf/ui/gallery/GalleryFragment.kt b/app/src/main/java/com/lhw/pdf/ui/gallery/GalleryFragment.kt index 1f55482..365474c 100644 --- a/app/src/main/java/com/lhw/pdf/ui/gallery/GalleryFragment.kt +++ b/app/src/main/java/com/lhw/pdf/ui/gallery/GalleryFragment.kt @@ -23,7 +23,7 @@ class GalleryFragment : Fragment() { savedInstanceState: Bundle? ): View { val galleryViewModel = - ViewModelProvider(this).get(GalleryViewModel::class.java) + ViewModelProvider(this)[GalleryViewModel::class.java] _binding = FragmentGalleryBinding.inflate(inflater, container, false) val root: View = binding.root diff --git a/app/src/main/java/com/lhw/pdf/ui/home/HomeFragment.kt b/app/src/main/java/com/lhw/pdf/ui/home/HomeFragment.kt index c24a05b..fed8f43 100644 --- a/app/src/main/java/com/lhw/pdf/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/lhw/pdf/ui/home/HomeFragment.kt @@ -23,7 +23,7 @@ class HomeFragment : Fragment() { savedInstanceState: Bundle? ): View { val homeViewModel = - ViewModelProvider(this).get(HomeViewModel::class.java) + ViewModelProvider(this)[HomeViewModel::class.java] _binding = FragmentHomeBinding.inflate(inflater, container, false) val root: View = binding.root diff --git a/app/src/main/java/com/lhw/pdf/ui/slideshow/SlideshowFragment.kt b/app/src/main/java/com/lhw/pdf/ui/slideshow/SlideshowFragment.kt index 04d0172..0c97486 100644 --- a/app/src/main/java/com/lhw/pdf/ui/slideshow/SlideshowFragment.kt +++ b/app/src/main/java/com/lhw/pdf/ui/slideshow/SlideshowFragment.kt @@ -23,7 +23,7 @@ class SlideshowFragment : Fragment() { savedInstanceState: Bundle? ): View { val slideshowViewModel = - ViewModelProvider(this).get(SlideshowViewModel::class.java) + ViewModelProvider(this)[SlideshowViewModel::class.java] _binding = FragmentSlideshowBinding.inflate(inflater, container, false) val root: View = binding.root