Commit 2b6ab242 authored by wanglei's avatar wanglei

...提交部分功能...

parent 07eacbb3
package com.base.superpdfreader.helps.pdf package com.base.superpdfreader.helps.pdf
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.base.superpdfreader.bean.PdfParameterBean import com.base.superpdfreader.bean.PdfParameterBean
import com.base.superpdfreader.bean.PdfParameterBean.Companion.arrayCompression
import com.base.superpdfreader.bean.PdfParameterBean.Companion.arrayMargin
import com.base.superpdfreader.bean.PdfParameterBean.Companion.arrayOrientation
import com.base.superpdfreader.bean.PdfParameterBean.Companion.arrayPageSize
import com.base.superpdfreader.helps.LogEx import com.base.superpdfreader.helps.LogEx
import com.tom_roush.pdfbox.pdmodel.PDDocument import com.tom_roush.pdfbox.pdmodel.PDDocument
import com.tom_roush.pdfbox.pdmodel.PDPage import com.tom_roush.pdfbox.pdmodel.PDPage
import com.tom_roush.pdfbox.pdmodel.PDPageContentStream import com.tom_roush.pdfbox.pdmodel.PDPageContentStream
import com.tom_roush.pdfbox.pdmodel.common.PDRectangle import com.tom_roush.pdfbox.pdmodel.common.PDRectangle
import com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject import com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject
import java.io.File import java.io.BufferedInputStream
import java.io.ByteArrayOutputStream
import java.io.FileInputStream
import java.io.IOException import java.io.IOException
object PdfHelp { object PdfHelp {
private val TAG = "PdfHelp" private val TAG = "PdfHelp"
fun convertImagesToPdf(imagePath: PdfParameterBean, pdfPath: String) { fun convertImagesToPdf(pdfParameterBean: PdfParameterBean, pdfPath: String) {
LogEx.logDebug(TAG, "orientation=${arrayOrientation[pdfParameterBean.orientation]}")
LogEx.logDebug(TAG, "pageSize=${arrayPageSize[pdfParameterBean.pageSize]}")
LogEx.logDebug(TAG, "margin=${arrayMargin[pdfParameterBean.margin]}")
LogEx.logDebug(TAG, "compression=${arrayCompression[pdfParameterBean.compression]}")
try { try {
PDDocument().use { document -> PDDocument().use { document ->
imagePath.pathArray.forEach { imagePath ->
val page = PDPage(PDRectangle.A4) // 创建一个 A4 大小的页面 pdfParameterBean.pathArray.forEach { imagePath ->
// val pdImage = PDImageXObject.createFromFileByExtension(File(imagePath), document)
val compress = when (pdfParameterBean.compression) {
0 -> 80
1 -> 60
2 -> 40
else -> 100
}
val byteArray = compressImage(imagePath, compress)
LogEx.logDebug(TAG, "compress size=${byteArray.size}")
val pdImage = PDImageXObject.createFromByteArray(document, byteArray, "")
//纸长大小
val rectangle = when (pdfParameterBean.pageSize) {
0 -> PDRectangle(pdImage.width.toFloat(), pdImage.height.toFloat())
1 -> PDRectangle.A4
2 -> PDRectangle.LETTER
else -> PDRectangle.A4
}
val page = PDPage(rectangle)
document.addPage(page) document.addPage(page)
val pdImage = PDImageXObject.createFromFileByExtension(File(imagePath), document) if (pdfParameterBean.orientation == 1) {
val pageSize = page.mediaBox page.rotation = 90
}
var x = 0f var x = 0f
var y = 0f var y = 0f
var scaledWidth = 0f var scaledWidth = 0f
var scaledHeight = 0f var scaledHeight = 0f
var needScale = false var needScale = false
val pageSize = page.mediaBox
var hMargin = 0
var vMargin = 0
if (pdfParameterBean.margin == 1) {
hMargin = 100
vMargin = 100
} else if (pdfParameterBean.margin == 2) {
hMargin = 200
vMargin = 200
}
if (pdImage.width > pageSize.width || pdImage.height > pageSize.height) { if (pdImage.width > pageSize.width + hMargin * 2 || pdImage.height > pageSize.height + vMargin * 2) {
// val scaleX = PDRectangle.A4.width / imageWidth // val scaleX = PDRectangle.A4.width / imageWidth
// val scaleY = PDRectangle.A4.height / imageHeight // val scaleY = PDRectangle.A4.height / imageHeight
needScale = true needScale = true
val scaleX = pageSize.width / pdImage.width val scaleX = pageSize.width / (pdImage.width + hMargin * 2)
val scaleY = pageSize.height / pdImage.height val scaleY = pageSize.height / (pdImage.height + vMargin * 2)
val scale = minOf(scaleX, scaleY) val scale = minOf(scaleX, scaleY)
scaledWidth = pdImage.width * scale scaledWidth = pdImage.width * scale
scaledHeight = pdImage.height * scale scaledHeight = pdImage.height * scale
...@@ -47,6 +94,7 @@ object PdfHelp { ...@@ -47,6 +94,7 @@ object PdfHelp {
x = (pageSize.width - pdImage.width) / 2f x = (pageSize.width - pdImage.width) / 2f
y = (pageSize.height - pdImage.height) / 2f y = (pageSize.height - pdImage.height) / 2f
} }
LogEx.logDebug(TAG, "x=$x y=$y") LogEx.logDebug(TAG, "x=$x y=$y")
LogEx.logDebug(TAG, "pageSize.width=${pageSize.width}") LogEx.logDebug(TAG, "pageSize.width=${pageSize.width}")
LogEx.logDebug(TAG, "pageSize.height=${pageSize.height}") LogEx.logDebug(TAG, "pageSize.height=${pageSize.height}")
...@@ -55,7 +103,6 @@ object PdfHelp { ...@@ -55,7 +103,6 @@ object PdfHelp {
LogEx.logDebug(TAG, "scaledWidth=${scaledWidth}") LogEx.logDebug(TAG, "scaledWidth=${scaledWidth}")
LogEx.logDebug(TAG, "scaledHeight=${scaledHeight}") LogEx.logDebug(TAG, "scaledHeight=${scaledHeight}")
PDPageContentStream(document, page).use { contentStream -> PDPageContentStream(document, page).use { contentStream ->
if (needScale) { if (needScale) {
contentStream.drawImage(pdImage, x, y, scaledWidth, scaledHeight) contentStream.drawImage(pdImage, x, y, scaledWidth, scaledHeight)
...@@ -72,4 +119,38 @@ object PdfHelp { ...@@ -72,4 +119,38 @@ object PdfHelp {
} }
} }
fun compressImage(path: String, compress: Int): ByteArray {
val bitmap = openImage2Bitmap(path)
val out = ByteArrayOutputStream()
var format = Bitmap.CompressFormat.JPEG
if (path.contains(".png")) {
format = Bitmap.CompressFormat.PNG
}
try {
out.flush()
out.close()
} catch (e: IOException) {
e.printStackTrace()
}
bitmap?.compress(format, compress, out)
return out.toByteArray()
}
fun openImage2Bitmap(imagePath: String): Bitmap? {
var bis: BufferedInputStream? = null
var bitmap: Bitmap? = null
try {
bis = BufferedInputStream(FileInputStream(imagePath))
bitmap = BitmapFactory.decodeStream(bis);
} catch (e: Exception) {
} finally {
bis?.close()
}
return bitmap
}
} }
\ No newline at end of file
...@@ -5,7 +5,9 @@ import android.app.Dialog ...@@ -5,7 +5,9 @@ import android.app.Dialog
import android.content.Context import android.content.Context
import android.view.LayoutInflater import android.view.LayoutInflater
import android.widget.TextView import android.widget.TextView
import android.widget.Toast
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import com.base.superpdfreader.R import com.base.superpdfreader.R
import com.base.superpdfreader.bean.PdfParameterBean import com.base.superpdfreader.bean.PdfParameterBean
import com.base.superpdfreader.bean.PdfParameterBean.Companion.arrayCompression import com.base.superpdfreader.bean.PdfParameterBean.Companion.arrayCompression
...@@ -40,6 +42,9 @@ object PDFSaveDialog { ...@@ -40,6 +42,9 @@ object PDFSaveDialog {
binding.tvMargin.text = arrayMargin[0] binding.tvMargin.text = arrayMargin[0]
binding.tvCompression.text = arrayCompression[0] binding.tvCompression.text = arrayCompression[0]
binding.ivDelete.setOnClickListener {
binding.editName.setText("")
}
binding.flOrientation.setOnClickListener { binding.flOrientation.setOnClickListener {
showPDFSaveParametersDialog(arrayOrientation) { index, text -> showPDFSaveParametersDialog(arrayOrientation) { index, text ->
pdfParameterBean.orientation = index pdfParameterBean.orientation = index
...@@ -72,6 +77,12 @@ object PDFSaveDialog { ...@@ -72,6 +77,12 @@ object PDFSaveDialog {
if (binding.flPassword.isVisible) { if (binding.flPassword.isVisible) {
pdfParameterBean.password = binding.editPassword.text.toString() pdfParameterBean.password = binding.editPassword.text.toString()
} }
val editName = binding.editName.text.toString()
if (editName.isEmpty()) {
Toast.makeText(this, "Name cannot be empty", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
pdfParameterBean.pdfName = editName
saveAction.invoke(pdfParameterBean) saveAction.invoke(pdfParameterBean)
dialog.dismiss() dialog.dismiss()
} }
......
...@@ -11,21 +11,23 @@ ...@@ -11,21 +11,23 @@
android:id="@+id/v_top" android:id="@+id/v_top"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="40dp" android:layout_height="40dp"
android:background="@color/white"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<com.github.barteksc.pdfviewer.PDFView <com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfview" android:id="@+id/pdfview"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:background="#FACCC6"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/v_top" /> app:layout_constraintTop_toBottomOf="@id/v_top" />
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/ll_tools" android:id="@+id/ll_tools"
android:visibility="gone"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@color/white" android:background="@color/white"
android:visibility="gone"
app:layout_constraintTop_toTopOf="@id/pdfview"> app:layout_constraintTop_toTopOf="@id/pdfview">
...@@ -104,6 +106,7 @@ ...@@ -104,6 +106,7 @@
android:id="@+id/rv_page" android:id="@+id/rv_page"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@color/white"
app:layout_constraintBottom_toBottomOf="parent" /> app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment