val bitmapHeight: Int =
if (webview.measuredHeight < webview.contentHeight) view.contentHeight else view.measuredHeight
val bitmap = Bitmap.createBitmap(webview.measuredWidth, bitmapHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
view.draw(canvas)
val scrollOffset =
if (scrollTo + view.getMeasuredHeight() > bitmap.height) bitmap.height else scrollTo
val resized = Bitmap.createBitmap(bitmap, 0, scrollOffset, bitmap.width, bitmap.contentHeight)
I want to let the user select the range of the screenshot to be taken of webview by scrolling.
How can I programmatically take a screenshot of a webview, capturing the full page?
I found the answer to this problem on the link above and implemented but it seems that the solution wasn't perfect.
The code above resulted in the extra white space being just added to the current range of view.
Related
I am trying to get the edit text bitmap by using createBitmap() but there is an error.
I have gone through different answers but they didn't work for me. Can anyone solve this issue?
Here is my code:
case : I am trying to place the edit text over the image just like a photo editor where we can edit and place the image
val bmp: Bitmap = Bitmap.createBitmap(binding.editImage.getDrawingCache())
editImage is the edit text where i can take the string from user
combineImages(test, bmp)
test is the URI bitmap that I am getting when I clicked the image from the camera.
private fun combineImages(background: Bitmap?, foreground: Bitmap?): Bitmap {
val matrix = Matrix()
matrix.setValues(floatArrayOf(1f, .5f, 0f, 0f, 1f, 0f, 0f, 0f, 1f))
var width = 0
var height = 0
width = background!!.width
height = background.height
val cs: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val comboImage = Canvas(cs)
val storage = Bitmap.createScaledBitmap(background, width, height, true)
comboImage.drawBitmap(storage,matrix, null)
if (foreground != null) {
comboImage.drawBitmap(foreground, matrix, null)
}
return cs
}
Please Help me!
Update
I have resolved this issue: Used the below answer
view.getDrawingCache() is deprecated in Android API 28
I am making sharing feature like youtube music shares image to instagram story. Everything works fine, but I can't make the curved edge.
The transparent part at the end of the edge changes to black like the below picture.
I tried
add bitmap.setHasAlpha(true)
custom shading with BitmapShader
set AntiAlias true
My code is like this
fun onCreate() {
val changedBitmap = cardViewToBitmap(binding.clCardview)
shareImage(changedBitmap)
}
fun cardViewToBitmap(cardView: View): Bitmap {
val bitmap = Bitmap.createBitmap(cardView.width, cardView.height, Bitmap.Config.ARGB_8888)
bitmap.eraseColor(Color.TRANSPARENT)
bitmap.setHasAlpha(true)
val canvas = Canvas(bitmap)
val radius = 20f
val path = Path()
path.addRoundRect(0f, 0f, cardView.width.toFloat(), cardView.height.toFloat(), radius, radius, Path.Direction.CW)
canvas.clipPath(path)
cardView.draw(canvas)
return bitmap
}
I found that instagram doesn't support transparent background. How can I do it?
I have a screen with a ScrollView. Then, I save the image of a view when I tap a button at the bottom of the scroll view. The problem I have is that I cannot save the upper part of the scrollView(which is off-screen) as an image. I am using PixelCopy with the following code:
fun takeScreenshot(view: View, activity: Activity){
val loc = IntArray(2)
view.getLocationInWindow(loc)
val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
PixelCopy.request(
activity.window,
Rect(loc[0], loc[1], loc[0] + view.width, loc[1] + view.height),
bitmap,
{if(it = PixelCopy.SUCCESS) //convert to png and save},
Handler(looper.getMainLooper())
)
I want to achieve the same image like what
val canvas = Canvas(bitmap)
view.draw(canvas)
can achieve where I can just save a screenshot of the entire view even if the other part of the view is not visible on the screen. Is there a way to achieve this using PixelCopy?
I have an imageView and several textViews
My app allows user to drag textViews on evey coordinates of imageView (imageView is not full screen) that user wants .
In other words this app allows user to add several captions to user image
and convert that image and captions to a single image and store it on user device.
According to one of stackOverFlow responses I can just convert one textView text to a bitamp
id there any way to screenshot from final image which user have created with its captions in kotlin??
This is my code:
#Throws(IOException::class)
fun foo(text: String) {
val textPaint = object : Paint() {
init {
setColor(Color.WHITE)
setTextAlign(Align.CENTER)
setTextSize(20f)
setAntiAlias(true)
}
}
val bounds = Rect()
textPaint.getTextBounds(text, 0, text.length, bounds)
val bmp = Bitmap.createBitmap(mImgBanner.getWidth(), mImgBanner.getHeight(), Bitmap.Config.RGB_565) //use ARGB_8888 for better quality
val canvas = Canvas(bmp)
canvas.drawText(text, 0, 20f, textPaint)
val path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/image.png"
val stream = FileOutputStream(path)
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream)
bmp.recycle()
stream.close()
}
Add desired views in xml layout inflate it and take screenshot of parent layout that is containing your views.
Code for taking screenshoot:
fun takeScreenshotOfView(view: View, height: Int, width: Int): Bitmap {
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val bgDrawable = view.background
if (bgDrawable != null) {
bgDrawable.draw(canvas)
} else {
canvas.drawColor(Color.WHITE)
}
view.draw(canvas)
return bitmap
}
you can also use the extension View.drawToBitmap(). It will return a Bitmap
/**
* Return a [Bitmap] representation of this [View].
*
* The resulting bitmap will be the same width and height as this view's current layout
* dimensions. This does not take into account any transformations such as scale or translation.
*
* Note, this will use the software rendering pipeline to draw the view to the bitmap. This may
* result with different drawing to what is rendered on a hardware accelerated canvas (such as
* the device screen).
*
* If this view has not been laid out this method will throw a [IllegalStateException].
*
* #param config Bitmap config of the desired bitmap. Defaults to [Bitmap.Config.ARGB_8888].
*/
fun View.drawToBitmap(config: Bitmap.Config = Bitmap.Config.ARGB_8888): Bitmap {
if (!ViewCompat.isLaidOut(this)) {
throw IllegalStateException("View needs to be laid out before calling drawToBitmap()")
}
return Bitmap.createBitmap(width, height, config).applyCanvas {
translate(-scrollX.toFloat(), -scrollY.toFloat())
draw(this)
}
}
Hi need to cut the certain portion from image view or while capturing image through camera. Like below
Please help me.
I tried and i got to crop the rectangle shape not for custom shape. Here is the code which i did for rectangle shape
val bh = bitmap.height
val bw = bitmap.width
val width = vF_atv_camera_preview!!.width;
val height = vF_atv_camera_preview!!.height;
val location = IntArray(2)
vL_atv_box!!.getLocationOnScreen(location);
val l = location[0] * bw / width
val t = location[1] * bh / height
val w = vL_atv_box!!.getWidth() * bw / width;
val h = vL_atv_box!!.getHeight() * bh / height;
val resizedBitmap = Bitmap.createBitmap(bitmap, l, t, w, h);
You can project the Camera2 Preview on a TextureView (which is more flexible and "manageable" than a SurfaceView) and then place a normal "ImageView" below it which has those two holes in its Drawable.
As you can see there is a small TextureView in the center with two ImageViews on its top and bottom showing a gradient (white-to-transparent Shape Drawable), but the Camera in portrait mode is filling the whole white space.