Last active
October 22, 2018 18:51
-
-
Save the-dagger/ccae617c982746e35b2f715db97c4bb6 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
class MainActivity : AppCompatActivity() { | |
private lateinit var renderScript: RenderScript | |
private lateinit var yuvToRGB: ScriptIntrinsicYuvToRGB | |
private var yuvDataLength: Int = 0 | |
private lateinit var allocationIn: Allocation | |
private lateinit var allocationOut: Allocation | |
private lateinit var bitmapOut: Bitmap | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
Fritz.configure(this) | |
cameraView.addFrameProcessor { frame -> | |
if (yuvDataLength == 0) { | |
//Run this only once | |
initializeData() | |
} | |
//Camera Preview returns NV21, so convert it to Bitmap : | |
//https://stackoverflow.com/a/43551798/5471095 | |
allocationIn.copyFrom(frame.data) //Take the NV21 format | |
yuvToRGB.forEach(allocationOut) //Convert it to Bitmap readable format | |
allocationOut.copyTo(bitmapOut) //Set it to the empty Bitmap created earlier | |
} | |
} | |
private fun initializeData(){ | |
yuvDataLength = cameraView.previewSize?.height!! * cameraView.previewSize?.width!! * 3 / 2 | |
renderScript = RenderScript.create(baseContext) | |
yuvToRGB = ScriptIntrinsicYuvToRGB.create(renderScript, Element.U8_4(renderScript)) | |
allocationIn = Allocation.createSized(renderScript, Element.U8(renderScript), yuvDataLength) | |
bitmapOut = Bitmap.createBitmap(cameraView.previewSize?.width!!, cameraView.previewSize?.height!!, Bitmap.Config.ARGB_8888) | |
allocationOut = Allocation.createFromBitmap(renderScript, bitmapOut) | |
yuvToRGB.setInput(allocationIn) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment