I am trying to achieve a fisheye effect on a BitMap image in Android. Is there an existing library or algorithm which can help?
Asked
Active
Viewed 3,793 times
3 Answers
7
I recommend you to use Android Media Effects API. If you want to have more control on the effect (or target older Android versions) you can also directly use opengl to apply a fisheye effect to your photo. Some tutorials on the subject : http://www.learnopengles.com/android-lesson-four-introducing-basic-texturing/ . Learning opengl will permit you to be able to apply all kind of effects to your photo, shader codes can be easily found on the internet (eg : https://github.com/BradLarson/GPUImage/tree/master/framework/Source)
Here is a shader code for a fisheye effect :
private static final String FISHEYE_FRAGMENT_SHADER =
"precision mediump float;\n" +
"uniform sampler2D u_Texture;\n" +
"uniform vec2 vScale;\n" +
"const float alpha = float(4.0 * 2.0 + 0.75);\n" +
"varying vec2 v_TexCoordinate;\n" +
"void main() {\n" +
" float bound2 = 0.25 * (vScale.x * vScale.x + vScale.y * vScale.y);\n" +
" float bound = sqrt(bound2);\n" +
" float radius = 1.15 * bound;\n" +
" float radius2 = radius * radius;\n" +
" float max_radian = 0.5 * 3.14159265 - atan(alpha / bound * sqrt(radius2 - bound2));\n" +
" float factor = bound / max_radian;\n" +
" float m_pi_2 = 1.570963;\n" +
" vec2 coord = v_TexCoordinate - vec2(0.5, 0.5);\n" +
" float dist = length(coord * vScale);\n" +
" float radian = m_pi_2 - atan(alpha * sqrt(radius2 - dist * dist), dist);\n" +
" float scalar = radian * factor / dist;\n" +
" vec2 new_coord = coord * scalar + vec2(0.5, 0.5);\n" +
" gl_FragColor = texture2D(u_Texture, new_coord);\n" +
"}\n";

Jacques Giraudel
- 352
- 4
- 19
-
What is the value of vScale here? – Rohit Patil Dec 16 '21 at 12:14
-
it is not fresh in my mind but I managed to make this effect work on this app : https://github.com/jacquesgiraudel/EffectNTransitionBrowser , see Frame.java – Jacques Giraudel Dec 17 '21 at 18:19
1
Perhaps a more simple solution would be using the Android Media Effects API. It's only available from API 14 and above however.

pilcrowpipe
- 2,528
- 6
- 29
- 41