paint-brush
如何构建您自己的 Android 移动 PDF 扫描工具by@foxitsoftware
7,522
7,522

如何构建您自己的 Android 移动 PDF 扫描工具

Foxit Software10m2023/11/07
Read on Terminal Reader

以下指南概述了如何利用福昕Android PDF SDK的强大功能来快速开发具有强大扫描功能的应用程序。无论您是拍摄多张照片还是从图库中选择图像,生成 PDF 都只需单击一下即可简单。该项目使您能够将扫描无缝集成到现有工作流程中,使其成为您的移动应用程序的宝贵补充。
featured image - 如何构建您自己的 Android 移动 PDF 扫描工具
Foxit Software HackerNoon profile picture
0-item
1-item
2-item


移动设备直接扫描 PDF 可以彻底改变您的文档工作流程。配备自动页面检测、内置闪光灯以提供更好的照明、精确的色彩管理以及为创建专业级 PDF 量身定制的相机设置等功能,您只需在智能手机上轻按几下即可获得卓越的效果,这要归功于尖端技术技术。


以下指南概述了如何利用福昕Android PDF SDK的强大功能来快速开发具有强大扫描功能的应用程序。无论您是拍摄多张照片还是从图库中选择图像,生成 PDF 都只需单击一下即可简单。该项目使您能够将扫描无缝集成到现有工作流程中,使其成为您的移动应用程序的宝贵补充。


您可以自由地轻松整合自己的业务或应用程序品牌,无需切换元素或重新设计用户界面。它提供了一个带有专用移动扫描按钮的空白 PDF 查看器,作为构建独特应用程序的基础。

系统要求

Android 设备要求:

  • Android 4.4 (API 19) 或更高版本

  • 32/64 位 ARM (armeabi-v7a/arm64-v8a) 或 32/64 位 Intel x86 CPU


  • Android Studio 3.2 或更高版本(支持 AndroidX)
    我们演示的运行环境:
    安卓工作室3.2
    JDK 1.8
    • Gradle 版本 4.6
    • Gradle 构建工具 3.2
    注意:从8.2版本开始,Foxit PDF SDK for Android将仅支持AndroidX,并且不再为Android支持库提供服务。

开发配置

启动 Android Studio 并打开 androidrdk/samples/viewer_ctrl_demo 项目。


启动 Android Studio

在gradle文件(view_ctrl_demo/app/build.gradle)中添加扫描依赖库,如下:


 implementation(name:'FoxitPDFScan-UI', ext:'aar') implementation(name:'FoxitMobileScanningRDK', ext:'aar') implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' 


添加依赖项

定义权限设置

在调用扫描功能之前,您需要获得存储权限和拍照权限。如果在调用Scan函数之前没有获得使用相机和相册的权限,应用程序将无法正常运行。请按照以下步骤设置权限。


使用以下代码在 AndroidManifest 文件中添加权限相关的声明:


 <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.VIBRATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.RUN_INSTRUMENTATION"/> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.CAMERA" />


然后在MainActivity.java中添加如下权限请求代码:


 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { int permission = ContextCompat.checkSelfPermission(this.getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE); if (permission != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE); @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_EXTERNAL_STORAGE) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { //Application permission is successful } else { UIToast.getInstance(getApplicationContext()).show(getString(R.string.fx_permission_denied)); finish(); } }


现在,我们添加与 Scan 功能相关的代码:


在 UI 中添加一个按钮来调用 Scan 功能。


添加 UI 按钮

首先,编写一些代码来添加相机按钮图标。您将定义按钮的布局、位置和图像资源。将此代码添加到 MainActivity 文件的 onCreate 方法中,以确保项目加载后立即显示该按钮。确保在之后添加此内容:


 protected void onCreate(Bundle savedInstanceState) { (...) // Make sure to add this block of code after all the already existing // view_ctrl_demo onCreate code is loaded mRootView=uiExtensionsManager.getContentView(); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); mIvScan = new ImageView(this); mIvScan.setImageResource(R.drawable.fx_floatbutton_scan); layoutParams.bottomMargin = 80; layoutParams.rightMargin = 50; layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); mIvScan.setLayoutParams(layoutParams); mRootView.addView(mIvScan); setContentView(mRootView); initScan(); }


现在,让我们为应用程序的扫描功能奠定基础。上面 onCreate 方法中的最后一行代码是对 initScan() 的调用,这是我们将用来初始化项目的相关类的方法。


下面的代码展示了如何从福昕的主要PDF扫描类(PDFScanManager)实例化一个对象,然后使用该类来初始化扫描仪和压缩功能。单击相机按钮后,最后一种方法将显示扫描功能。


 PDFScanManager pdfScanManager = PDFScanManager.instance() if (!PDFScanManager.isInitializeScanner()) { long framework1 = 0; long framework2 = 0; PDFScanManager.initializeScanner(App.inst().getAppActivity().getApplication(), framework1, framework2); } if (!PDFScanManager.isInitializeCompression()) { long compression1 = 0; long compression2 = 0; PDFScanManager.initializeCompression(App.inst().getAppActivity().getApplication(), compression1, compression2); } if (PDFScanManager.isInitializeScanner() && PDFScanManager.isInitializeCompression()) { //Loaded Successfully pdfScanManager.showUI(activity.this); }


然后将初始化代码添加到 initScan 方法中,如下所示。该方法也将被添加到 MainActivity 类中,并在项目加载后立即调用。这可确保您的项目拥有所有可用的扫描库和功能。


该项目需要以下导入和变量:


 import com.foxit.pdfscan.PDFScanManager; private ImageView mIvScan; private ViewGroup mRootView; private void initScan(){ final PDFScanManager pdfScanManager = PDFScanManager.instance(); mIvScan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!PDFScanManager.isInitializeScanner()) { long framework1 = 0; long framework2 = 0; PDFScanManager.initializeScanner(getApplication(), framework1, framework2); } if (!PDFScanManager.isInitializeCompression()) { long compression1 = 0; long compression2 = 0; PDFScanManager.initializeCompression(getApplication(), compression1, compression2); } if (PDFScanManager.isInitializeScanner() && PDFScanManager.isInitializeCompression()) { pdfScanManager.showUI((Activity) mContext); } else { UIToast.getInstance(getApplicationContext()) .show(AppResource.getString(getApplicationContext(), R.string.rv_invalid_license)); } } }); }


现在我们已经初始化了库并且已经奠定了基础,我们将编写两个基本回调接口,用于在扫描后保存文档:第一个是 ScanPDFDoneCallBack,它是完全自定义的,第二个是 IPDFScanManagerListener,它在 SDK 中开箱即用。请参阅下面的两个内容:


 public interface ScanPDFDoneCallBack { /** Success, and no error occurs */ int e_ErrSuccess = 0; /** Failed, any unknown error occurs. */ int e_ErrUnknown = 1; /** * After saving PDF successfully, return to the saving path * * @param errorCode if the scanned file is successfully saved as a pdf document, the errorCode is {@link #e_ErrSuccess}, otherwise is {@link #e_ErrUnknown} * @param savePath the document file path. */ void doneCallBack(int errorCode, String savePath); } public interface IPDFScanManagerListener { /** * Success, and no error occurs */ int e_ErrSuccess = 0; /** * Failed, any unknown error occurs. */ int e_ErrUnknown = 1; /** * Called when if the scanned file is successfully saved as a pdf document. * * @param errorCode if the scanned file is successfully saved as a pdf document, the errorCode is {@link #e_ErrSuccess}, otherwise is {@link #e_ErrUnknown} * @param path the document file path. */ void onDocumentAdded(int errorCode, String path); }


两个界面的区别在于,如果设置了ScanPDFDoneCallBack的监听,当点击下图右下角的“完成”按钮时,会直接将文档保存到默认路径并退出扫描界面,并且通过参数savePath返回默认保存路径。


如果没有设置ScanPDFDoneCallBack,当点击下图右下角的“完成”按钮时,会弹出选择保存目录的界面。用户选择指定的目录,文档将保存在用户选择的目录中。


https://developers.foxit.com/dk_utwp/2021/10/android-mobile-scanning-tool-pdf-sdk-app.mp4


现在,您必须通过将接口添加到 MainActivity.java 来实现这些接口。


 pdfScanManager.setDoneCallBack(new ScanPDFDoneCallBack() { @Override public void doneCallBack(int errorCode, final String savePath) { if (errorCode == e_ErrSuccess) { //The document was saved successfully } } }); PDFScanManager.registerManagerListener(new IPDFScanManagerListener() { @Override public void onDocumentAdded(int errorCode, String path) { if (errorCode == e_ErrSuccess) { //The document was saved successfully } } });


如果您想实现pdfscan类的更多接口,请参考foxitpdfsdk_8_1_android/docs中的API参考文档。


完毕!扫描功能已成功添加到您的应用程序中。运行应用程序,单击您之前创建的扫描按钮并开始扫描体验。


我们与您合作,将完整的 PDF 库功能添加到您的项目中,并在一个核心 API 上跨所有平台和环境进行开发。 立即点击此处进行 30 天免费试用。


也发布在这里