package ir.programchi
import android.graphics.Bitmap
import android.graphics.Canvas
import android.os.Environment
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import java.io.File
import java.io.FileOutputStream
import java.util.Random
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bitmap = loadBitmapFromView(findViewById(R.id.activity_main), 350, 450)
saveImage(bitmap)
}
companion object {
fun saveImage(bitmap: Bitmap) {
val root = Environment.getExternalStorageDirectory().toString()
val myDir = File(root + "/req_images")
myDir.mkdirs()
val generator = Random()
var n = 10000
n = generator.nextInt(n)
val fname = "Image-$n.jpg"
val file = File(myDir, fname)
// Log.i(TAG, "" + file);
if (file.exists())
file.delete()
try {
val out = FileOutputStream(file)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)
out.flush()
out.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
fun loadBitmapFromView(v: View, width: Int, height: Int): Bitmap {
val b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val c = Canvas(b)
v.layout(0, 0, v.layoutParams.width, v.layoutParams.height)
v.draw(c)
return b
}
}
}