Created
May 29, 2018 04:39
-
-
Save xuhaibahmad/7ac270ebba69b1535799037c97d620dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.zuhaibahmad.template.utils | |
import android.content.Context | |
import android.graphics.Bitmap | |
import android.media.MediaScannerConnection | |
import android.os.Environment | |
import android.util.Log | |
import android.view.View | |
import com.itextpdf.text.Document | |
import com.itextpdf.text.DocumentException | |
import com.itextpdf.text.Image | |
import com.itextpdf.text.pdf.PdfWriter | |
import com.zuhaibahmad.template.AppConstants | |
import com.zuhaibahmad.template.R | |
import java.io.ByteArrayOutputStream | |
import java.io.File | |
import java.io.FileNotFoundException | |
import java.io.FileOutputStream | |
import java.io.IOException | |
import java.text.SimpleDateFormat | |
import java.util.* | |
/** | |
* Created by Zuhaib Ahmad on 6/10/2017. | |
* | |
* | |
* Utility class with helper methods to convert Xml layouts | |
* ([View Groups][android.view.ViewGroup] into PDF files | |
* using iTextPdf library | |
*/ | |
object XmlToPdfUtils { | |
private const val SCREENSHOT_QUALITY = 100 | |
private const val PERCENT = 100 | |
/** | |
* Generates a [PDF Document][Document] from provided bitmap on the supplied location | |
* | |
* @param path The path to generate the PDF file | |
* @param screenshot The bitmap to be converted into PDF | |
* @throws IOException The file not found exception | |
* @throws DocumentException The fail to write to document exception | |
*/ | |
@Throws(IOException::class, DocumentException::class) | |
fun generate(path: String, screenshot: Bitmap) { | |
val document = Document() | |
val stream = ByteArrayOutputStream() | |
PdfWriter.getInstance(document, FileOutputStream(path)) | |
document.open() | |
screenshot.compress(Bitmap.CompressFormat.PNG, SCREENSHOT_QUALITY, stream) | |
val byteArray = stream.toByteArray() | |
val image = Image.getInstance(byteArray) | |
val percent = (document.pageSize.width - document.leftMargin() - | |
document.rightMargin()) / screenshot.width * PERCENT | |
image.scalePercent(percent) | |
document.add(image) | |
document.close() | |
} | |
/** | |
* Take screen shot of the View with spaces as per constraints | |
* | |
* @param v the v | |
* @return the bitmap | |
*/ | |
fun takeScreenShotOfView(v: View): Bitmap { | |
v.isDrawingCacheEnabled = true | |
v.buildDrawingCache(true) | |
// creates immutable clone | |
val b = Bitmap.createBitmap(v.drawingCache) | |
v.isDrawingCacheEnabled = false // clear drawing cache | |
return b | |
} | |
/** | |
* Take screen shot of the View with spaces as per constraints | |
* | |
* @param v The view to be used for screenshot | |
* @param width The width of view | |
* @param height The height of view | |
* @return the bitmap | |
*/ | |
fun takeScreenShotOfView(v: View, width: Int, height: Int): Bitmap { | |
v.isDrawingCacheEnabled = true | |
v.buildDrawingCache(true) | |
// creates immutable clone | |
val b = Bitmap.createBitmap(v.drawingCache, 0, 0, width, height) | |
v.isDrawingCacheEnabled = false // clear drawing cache | |
return b | |
} | |
/** | |
* Take screen shot of root view. | |
* | |
* @param v the v | |
* @return the bitmap | |
*/ | |
fun takeScreenShotOfRootView(v: View): Bitmap { | |
return takeScreenShotOfView(v.rootView) | |
} | |
/** | |
* Take screen shot of just the View without any constraints | |
* | |
* @param v the v | |
* @return the bitmap | |
*/ | |
fun takeScreenShotOfJustView(v: View): Bitmap { | |
v.measure( | |
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), | |
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) | |
) | |
v.layout(0, 0, v.measuredWidth, v.measuredHeight) | |
return takeScreenShotOfView(v) | |
} | |
/** | |
* Save screenshot to pictures folder. | |
* | |
* @param context the activity | |
* @param image the image | |
* @param filename the filename | |
*/ | |
fun saveScreenshotToPicturesFolder(context: Context, image: Bitmap, filename: String) { | |
val bitmapFile = getOutputMediaFile(filename) | |
if (bitmapFile == null) { | |
Log.d( | |
AppConstants.TAG, | |
context.getString(R.string.err_msg_save_screenshot) | |
)// e.getMessage()); | |
return | |
} | |
try { | |
val fos = FileOutputStream(bitmapFile) | |
image.compress(Bitmap.CompressFormat.PNG, SCREENSHOT_QUALITY, fos) | |
fos.close() | |
// Initiate media scanning to make the image available in gallery apps | |
MediaScannerConnection.scanFile( | |
context, arrayOf(bitmapFile.path), | |
arrayOf("image/jpeg"), null | |
) | |
} catch (e: FileNotFoundException) { | |
Log.d(AppConstants.TAG, "File not found: " + e.message) | |
} catch (e: IOException) { | |
Log.d(AppConstants.TAG, "Error accessing file: " + e.message) | |
} | |
} | |
/** | |
* Generates jpg file of the screenshot bitmap | |
* | |
* @param filename The name for the file to save | |
* @return The jpg file for screenshot | |
*/ | |
private fun getOutputMediaFile(filename: String): File? { | |
val type = Environment.DIRECTORY_PICTURES | |
val picturesDirectory = Environment.getExternalStoragePublicDirectory(type) | |
val directory = File(picturesDirectory.toString() + File.separator) | |
// Create the storage directory if it does not exist | |
if (!directory.exists() && !directory.mkdirs()) return null | |
// Create a media file name | |
val temp = "ddMMyyyy_HHmm" | |
val timeStamp = SimpleDateFormat(temp, Locale.getDefault()).format(Date()) | |
val path = directory.path + File.separator + filename + timeStamp + ".jpg" | |
return File(path) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment