You do not need any separate function for this. OpenCV comes with square detection (which actually defines rectangles, you can add a constraint so that all sides are equal in length to get a square).
Check out this link: squares.cpp
, SOF: javacv?
, .
