Skip to content

Commit

Permalink
Scanbot migration 1.89.0 -> 4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
surinder-tsys committed Feb 8, 2024
1 parent 96c9552 commit aa6fb43
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 280 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ dependencies {
// splash screen dependency ref: https://developer.android.com/develop/ui/views/launch/splash-screen/migrate
implementation 'androidx.core:core-splashscreen:1.0.1'

//scanbot sdk
//scanbot sdk: https://github.com/doo/scanbot-sdk-example-android
implementation "io.scanbot:sdk-package-2:$scanbotSdkVersion"

//apache pdf-box for encrypting pdf files
Expand Down
33 changes: 17 additions & 16 deletions app/src/main/java/com/nmc/android/jobs/ScanDocUploadWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.operations.UploadFileOperation
import com.owncloud.android.ui.notifications.NotificationUtils
import com.owncloud.android.utils.StringUtils
import io.scanbot.ocr.model.OcrPage
import io.scanbot.pdf.model.PageSize
import io.scanbot.pdf.model.PdfConfig
import io.scanbot.sdk.ScanbotSDK
import io.scanbot.sdk.core.contourdetector.DetectionResult
import io.scanbot.sdk.entity.Language
import io.scanbot.sdk.core.contourdetector.DetectionStatus
import io.scanbot.sdk.ocr.OpticalCharacterRecognizer
import io.scanbot.sdk.ocr.process.OcrResult
import io.scanbot.sdk.persistence.Page
import io.scanbot.sdk.persistence.PageFileStorage
import io.scanbot.sdk.process.PDFPageSize
import io.scanbot.sdk.process.PDFRenderer
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.pdmodel.encryption.AccessPermission
Expand All @@ -37,7 +38,7 @@ import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.security.SecureRandom

class ScanDocUploadWorker constructor(
class ScanDocUploadWorker(
private val context: Context,
params: WorkerParameters,
private val notificationManager: NotificationManager,
Expand Down Expand Up @@ -78,15 +79,19 @@ class ScanDocUploadWorker constructor(
SaveScannedDocumentFragment.SAVE_TYPE_JPG -> {
saveJPGImageFiles(docFileName, bitmapList)
}

SaveScannedDocumentFragment.SAVE_TYPE_PNG -> {
savePNGImageFiles(docFileName, bitmapList)
}

SaveScannedDocumentFragment.SAVE_TYPE_PDF -> {
saveNonOCRPDFFile(docFileName, bitmapList, scanDocPdfPwd)
}

SaveScannedDocumentFragment.SAVE_TYPE_PDF_OCR -> {
savePDFWithOCR(docFileName, bitmapList, scanDocPdfPwd)
}

SaveScannedDocumentFragment.SAVE_TYPE_TXT -> {
saveTextFile(docFileName, bitmapList)
}
Expand Down Expand Up @@ -148,7 +153,8 @@ class ScanDocUploadWorker constructor(
private fun saveNonOCRPDFFile(fileName: String?, bitmapList: List<Bitmap>, pdfPassword: String?) {

val pageList = getScannedPages(bitmapList)
val pdfFile: File? = pdfRenderer.renderDocumentFromPages(pageList, PDFPageSize.A4)
val pdfFile: File? =
pdfRenderer.renderDocumentFromPages(pageList, PdfConfig.defaultConfig().copy(pageSize = PageSize.A4))
if (pdfFile != null) {
val renamedFile = File(pdfFile.parent + OCFile.PATH_SEPARATOR + fileName + ".pdf")
if (pdfFile.renameTo(renamedFile)) {
Expand Down Expand Up @@ -200,21 +206,19 @@ class ScanDocUploadWorker constructor(
private fun getScannedPages(bitmapList: List<Bitmap>): List<Page> {
val pageList: MutableList<Page> = ArrayList()
for (bitmap in bitmapList) {
val page = Page(pageFileStorage.add(bitmap), emptyList(), DetectionResult.OK)
val page = Page(pageFileStorage.add(bitmap), listOf(), DetectionStatus.OK)
pageList.add(page)
}
return pageList
}

private fun savePDFWithOCR(fileName: String?, bitmapList: List<Bitmap>, pdfPassword: String?) {
val languages = setOf(Language.ENG)
val ocrResult: OcrResult =
opticalCharacterRecognizer.recognizeTextWithPdfFromPages(
getScannedPages(bitmapList),
PDFPageSize.A4,
languages
PdfConfig.defaultConfig().copy(pageSize = PageSize.A4)
)
val ocrPageList: List<OcrResult.OCRPage> = ocrResult.ocrPages
val ocrPageList: List<OcrPage> = ocrResult.ocrPages
if (ocrPageList.isNotEmpty()) {
val ocrText = ocrResult.recognizedText
}
Expand All @@ -229,18 +233,17 @@ class ScanDocUploadWorker constructor(
}

private fun saveTextFile(fileName: String?, bitmapList: List<Bitmap>) {
val languages = setOf(Language.ENG)
for (i in bitmapList.indices) {
var newFileName = fileName
val bitmap = bitmapList[i]
if (i > 0) {
newFileName += "($i)"
}
val page = Page(pageFileStorage.add(bitmap), emptyList(), DetectionResult.OK)
val page = Page(pageFileStorage.add(bitmap), emptyList(), DetectionStatus.OK)
val pageList: MutableList<Page> = ArrayList()
pageList.add(page)
val ocrResult: OcrResult = opticalCharacterRecognizer.recognizeTextFromPages(pageList, languages)
val ocrPageList: List<OcrResult.OCRPage> = ocrResult.ocrPages
val ocrResult: OcrResult = opticalCharacterRecognizer.recognizeTextFromPages(pageList)
val ocrPageList: List<OcrPage> = ocrResult.ocrPages
if (ocrPageList.isNotEmpty()) {
val ocrText = ocrResult.recognizedText
val txtFile = FileUtils.writeTextToFile(context, ocrText, newFileName)
Expand All @@ -256,11 +259,9 @@ class ScanDocUploadWorker constructor(
}

FileUploader.uploadNewFile(
context,
accountManager.user,
savedFiles.toTypedArray(),
remotePaths,
null, // MIME type will be detected from file name
FileUploader.LOCAL_BEHAVIOUR_DELETE,
false, // do not create parent folder if not existent
UploadFileOperation.CREATED_BY_USER,
Expand Down
50 changes: 24 additions & 26 deletions app/src/main/java/com/nmc/android/ui/CropScannedDocumentFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import com.nmc.android.utils.ScanBotSdkUtils
import com.owncloud.android.R
import com.owncloud.android.databinding.FragmentCropScanBinding
import io.scanbot.sdk.ScanbotSDK
import io.scanbot.sdk.core.contourdetector.DetectionResult
import io.scanbot.sdk.core.contourdetector.ContourDetector
import io.scanbot.sdk.core.contourdetector.DetectionStatus
import io.scanbot.sdk.core.contourdetector.Line2D
import io.scanbot.sdk.process.CropOperation
import io.scanbot.sdk.process.ImageProcessor
import java.util.concurrent.Executors
import kotlin.math.absoluteValue

Expand All @@ -29,11 +31,12 @@ class CropScannedDocumentFragment : Fragment() {
private lateinit var onFragmentChangeListener: OnFragmentChangeListener
private lateinit var onDocScanListener: OnDocScanListener

private var scannedDocIndex: Int = -1
private lateinit var scanbotSDK: ScanbotSDK
private lateinit var imageProcessor: ImageProcessor
private lateinit var contourDetector: ContourDetector

private var scannedDocIndex: Int = -1
private lateinit var originalBitmap: Bitmap

private var rotationDegrees = 0
private var polygonPoints: List<PointF>? = null

Expand Down Expand Up @@ -72,6 +75,9 @@ class CropScannedDocumentFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
scanbotSDK = (requireActivity() as ScanActivity).scanbotSDK
contourDetector = scanbotSDK.createContourDetector()
imageProcessor = scanbotSDK.imageProcessor()

detectDocument()
binding.cropBtnResetBorders.setOnClickListener {
onClickListener(it)
Expand Down Expand Up @@ -155,28 +161,26 @@ class CropScannedDocumentFragment : Fragment() {
InitImageViewTask().executeOnExecutor(Executors.newSingleThreadExecutor())
}

// We use AsyncTask only for simplicity here. Avoid using it in your production app due to memory leaks, etc!
@SuppressLint("StaticFieldLeak")
internal inner class InitImageViewTask : AsyncTask<Void?, Void?, InitImageResult>() {
private var previewBitmap: Bitmap? = null

override fun doInBackground(vararg params: Void?): InitImageResult {
//originalBitmap = FileUtils.convertFileToBitmap(File(scannedDocPath))
originalBitmap = onDocScanListener.scannedDocs[scannedDocIndex]
previewBitmap = ScanBotSdkUtils.resizeForPreview(originalBitmap)

val detector = scanbotSDK.createContourDetector()
val detectionResult = detector.detect(originalBitmap)
val linesPair = Pair(detector.horizontalLines, detector.verticalLines)
val polygon = detector.polygonF

return when (detectionResult) {
DetectionResult.OK,
DetectionResult.OK_BUT_BAD_ANGLES,
DetectionResult.OK_BUT_TOO_SMALL,
DetectionResult.OK_BUT_BAD_ASPECT_RATIO -> {
InitImageResult(linesPair, polygon!!)
val result = contourDetector.detect(originalBitmap)
return when (result?.status) {
DetectionStatus.OK,
DetectionStatus.OK_BUT_BAD_ANGLES,
DetectionStatus.OK_BUT_TOO_SMALL,
DetectionStatus.OK_BUT_BAD_ASPECT_RATIO -> {
val linesPair = Pair(result.horizontalLines, result.verticalLines)
val polygon = result.polygonF

InitImageResult(linesPair, polygon)
}

else -> InitImageResult(Pair(listOf(), listOf()), listOf())
}
}
Expand All @@ -190,7 +194,7 @@ class CropScannedDocumentFragment : Fragment() {
binding.cropPolygonView.polygon = initImageResult.polygon
binding.cropPolygonView.setLines(initImageResult.linesPair.first, initImageResult.linesPair.second)

if (initImageResult.polygon.isNullOrEmpty()) {
if (initImageResult.polygon.isEmpty()) {
resetCrop()
} else {
onCropDragListener()
Expand All @@ -204,7 +208,7 @@ class CropScannedDocumentFragment : Fragment() {
// crop & warp image by selected polygon (editPolygonView.getPolygon())
val operations = listOf(CropOperation(binding.cropPolygonView.polygon))

var documentImage = scanbotSDK.imageProcessor().processBitmap(originalBitmap, operations, false)
var documentImage = imageProcessor.processBitmap(originalBitmap, operations, false)
documentImage?.let {
if (rotationDegrees > 0) {
// rotate the final cropped image result based on current rotation value:
Expand All @@ -213,17 +217,11 @@ class CropScannedDocumentFragment : Fragment() {
documentImage = Bitmap.createBitmap(it, 0, 0, it.width, it.height, matrix, true)
}
onDocScanListener.replaceScannedDoc(scannedDocIndex, documentImage, false)
/* onDocScanListener.replaceScannedDoc(
scannedDocIndex, FileUtils.saveImage(
requireContext(),
documentImage, null
)
)*/

onFragmentChangeListener.onReplaceFragment(
EditScannedDocumentFragment.newInstance(scannedDocIndex), ScanActivity
.FRAGMENT_EDIT_SCAN_TAG, false
.FRAGMENT_EDIT_SCAN_TAG, false
)
// resultImageView.setImageBitmap(resizeForPreview(documentImage!!))
}
}

Expand Down
Loading

0 comments on commit aa6fb43

Please sign in to comment.