Last active
August 6, 2018 10:46
-
-
Save Sren-Hmkn/e992ed60dae59278eea0dc8ba2446c4f to your computer and use it in GitHub Desktop.
Adjusting rotation and scale of the camera
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
protected void deliverAndDrawFrame(CvCameraViewFrame frame) { | |
Mat modified; | |
if (mListener != null) { | |
modified = mListener.onCameraFrame(frame); | |
} else { | |
modified = frame.rgba(); | |
} | |
boolean bmpValid = true; | |
if (modified != null) { | |
try { | |
Utils.matToBitmap(modified, mCacheBitmap); | |
} catch(Exception e) { | |
Log.e(TAG, "Mat type: " + modified); | |
Log.e(TAG, "Bitmap type: " + mCacheBitmap.getWidth() + "*" + mCacheBitmap.getHeight()); | |
Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage()); | |
bmpValid = false; | |
} | |
} | |
if (bmpValid && mCacheBitmap != null) { | |
Canvas canvas = getHolder().lockCanvas(); | |
if (canvas != null) { | |
canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR); | |
if (BuildConfig.DEBUG) | |
Log.d(TAG, "mStretch value: " + mScale); | |
// rotation part | |
canvas.save(); | |
canvas.rotate(90, (canvas.getWidth()/2),(canvas.getHeight()/2)); | |
mScale = 1.7f; | |
// rotation part ended | |
if (mScale != 0) { | |
canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()), | |
new Rect((int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2), | |
(int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2), | |
(int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2 + mScale*mCacheBitmap.getWidth()), | |
(int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2 + mScale*mCacheBitmap.getHeight())), null); | |
} else { | |
canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()), | |
new Rect((canvas.getWidth() - mCacheBitmap.getWidth()) / 2, | |
(canvas.getHeight() - mCacheBitmap.getHeight()) / 2, | |
(canvas.getWidth() - mCacheBitmap.getWidth()) / 2 + mCacheBitmap.getWidth(), | |
(canvas.getHeight() - mCacheBitmap.getHeight()) / 2 + mCacheBitmap.getHeight()), null); | |
} | |
if (mFpsMeter != null) { | |
mFpsMeter.measure(); | |
mFpsMeter.draw(canvas, 20, 30); | |
} | |
// for the rotation part | |
canvas.restore(); | |
// for the rotation part ended | |
getHolder().unlockCanvasAndPost(canvas); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment