Use OpenCV to show camera on android App with correct orientation

Return to OpenCV 4 Android Tutorials List

In this series of tutorials, you are learning OpenCV for Android using Android Studio. So far, you have made a working OpenCV Android Studio Project containing OpenCV module.

In Here is the overview of what we are going to achieve:

  • Create an OpenCV for Android Project (As in previous tutorial in this series) (Here)
  • Show Camera on the Screen
    1. Edit Manifest
      1. Add permission to allow camera.
      2. Support various screen sizes and adaptability of camera.
      3. Use specific features of the camera.
    2. Add a custom Layout show_camera.xml to display camera in it.
    3. Edit 
      1. Import required android classes
      2.  Import OpenCV classes
      3. Connect to OpenCV manager
      4. Initiate LogCat to log events of out app
      6. Corrected Orientation (portrait)

Trust me, it is very simple to do. Lets do it!



We need to edit the AndroidManifest.xml file like following:

  1. Allow permission to use camera
  2. Allow camera to optimize to the dimensions of the device in use
  3. Use Front camera and autofocus of camera :S

The Manifest.xml file in its entirety is given here


This entire process is given here:

Show camera on android App using OpenCV for Android - Edit Manifest

This is all you need to do in AndroidManifest.xml

Create a new layout (show_camera.xml )

Now in your project, do the following:

  1. Create a new layout called show_camera.xml
  2. Add the following code it to:

    Note that the JavaCameraView has the id of show_camera_activity_java_surface_view

This is given below:

Import the following Android Classes in your MainActivity_show_camera:

Import the following OpenCV classes in your MainActivity_show_camera:

Now, lets work with the MainActivity_show_camera Class 🙂 (Code in the end). I will explain every line of OpenCV code in detail.

  • Implement the OpenCV class CvCameraViewListener2 to allow OpenCV to communicate with android camera functionalities.

  • Declare the following items in the class
  • Now, lets call OpenCV manager to help our app communicate with android phone to make OpenCV work

    • CameraBridgeViewBase mOpenCvCameraView
      This variable acts as a bridge between camera and OpenCV library.
    • BaseLoaderCallback mLoaderCallback = …..
      Well, once OpenCV library is loaded, you may want to perform some actions. For example, displaying a success or failure message.
  • Initiate the LogCat
  • Now, when the activity is created, display the OpenCV camera in the layout. show_camera.xml.
  •  The following three functions handle the events when the app is Paused, Resumed and Closed/Destroyed
  • Now, we will do two main things:
    • Receive Image Data when the camera preview starts on your screen 😀
    • Destroy image data when you stop camera preview on your phone screen
    • Now, this one is interesting! OpenCV orients the camera to left by 90 degrees. So if the app is in portrait more, camera will be in -90 or 270 degrees orientation. We fix that in the next and the most important function. There you go!

The entire code is given below:

The entire process of editing is shown here:

Show camera on android App using OpenCV for Android - MainActivity_show_camera

Note: The camera disorientation problem is a bitch an issue not to be takien lightheartedly. Thanks for noting.

This concludes this tutorial, now that you have created a camera app, it is now time to continue this and perform more Computer Vision! In the next tutorial, I will draw a simple object on the screen. Be sure to check out the list of OpenCV for Android tutorials for latest tutorials.

Thanks for reading, and please share and comment if this helped and to share your ideas and opinions.

Return to OpenCV 4 Android Tutorials List

Codeonion contributes to the honorable mission of “helping people learn and earn online”. Whether your day job is creative or not, life is better when you learn and apply creative skills regularly. To do this is satisfying in its own right and a practical investment in your future.

Comments (64)

  1. Anonymous says:


    mOpenCvCameraView = (JavaCameraView) findViewById(;

    O que é esse R

  2. Angelo says:

    Thank you!!! this help me a lot!!!

  3. Vino says:

    Thanks for nice tutorial.But,I want to know how to run my projects without installing openCV Manager.

  4. Paul says:

    Can someone explain, why we are adding 3 objects with type “Mat”, and how exactly we use it when rotate the camera?

  5. Hi! This is a great tutorial! But I would like to open my camera via button. Like the main activity would be a button and when pressed, it will open the camera. I tried the intent but when I build the apk, the whole app won’t open anymore. This is kind of urgent so an immediate help would be a great help. Thank you so much!

  6. Eric says:

    Why is the ‘this’ in mOpenCvCameraView.setCvCameraViewListener(this); underlined in red?

  7. USMAN AHMEd says:

    Very nice work. step by step guide helps alot thank you so much

  8. dama says:

    hi thanks for nice tutorial. but i got a problem. code is working fine but the camera view is stretched. i mean it just look like aspect ration issue. what may be the solution for this? thank you.

  9. Anonymous says:


    Thanks for the great blog post. I found it extremely helpful.

    I just wanted to draw to your attention a small issue. I am not sure if I missed something in the instructions, but, here’s the issue: The portrait orientation works fine, however, when rotating the phone to landscape (horizontal) mode, the orientation is now incorrect. The view is also stretched when switching to landscape mode.

    Everything else seems to work okay. Loved the tutorial and the detailed explanations for everything. I would have loved to read about a way to make the portrait mode full-screen.

    Again, thanks for sharing the awesome informational blog post.

  10. Chiara says:

    Thanks for making this tutorial. 🙂
    I’have a problem, I’m using Android Studio 2.3.3 and I’m using Android emulator (Pixel API 26). When run the application (0 error 0 warning) on emulator appears this message:
    “Package not found
    OpenCV Manager package was not found! Try to install it?”
    If I select “yes”, the message becomes
    “OpenCv Manager
    Package installation failed”

    What could be the problem?
    How can I fix it?

    Thanks 🙂

    1. codeonion says:

      Well Chiara, I never test my apps on Emulator, BUT!

      Try this:

      1. Open Emulator
      2. Go to terminal
      3. Go to go to your SDK tool/ directory
      4. Run this command : adb install /OpenCV-


      I hope you find this helpful.

  11. cdaniel says:

    What about switching to front camera? I can switch with this but image is then upside down.

    1. Anonymous says:

      How do you do this?

  12. Jun says:

    Why does it crashes when i try to run on my phone? I did everything

    1. Jun says:

      It says Unfortunately, OpenCV_test has stopped.

    2. Jun says:

      Process: com.example.junlong.opencv_test, PID: 16704
      java.lang.RuntimeException: Unable to resume activity {com.example.junlong.opencv_test/com.example.junlong.opencv_test.MainActivity_show_camera}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=org.opencv.engine.BIND }

    3. Jun says:

      I changed targetSdkVersion to 19 and it opens. However, it says OpenCV was not initialized correctly. Application will be shut down.

      1. codeonion says:

        Have you installed OpenCV manager in your Android Device?

  13. Redo says:


    For me by default the show_camera_activity_java_surface_view is weirdly transformed , and has a weird resolution and is not full screen, although the parameters are set to :”fill_parent”.. I tried the full screen orientation solution from one of these comments but that only works if i rotate the device.

    So my question would be that do you know a way to make it full screen by default , so its not transformed weirdly?

    1. Redo says:

      Ok well easy fix (sort of — since not exactly what i wanted, but works) i set screen orientation to landscape, and removed the 90 degrees rotation, and now works fine…

      But now i started manipulating the mats, and wanted to save them, but if i use the mainthread, then it does too much work, and receives too many new mats, and if i use a new thread for some reason it never finishes what it should do…

      My guess is that this has something to do with memory management, and the fact that i’m getting waay more mats than required..

      Any idea on how i can reduce the number of mats from the camera?

  14. Dan says:

    Hey, great simple example. Helped a lot. Thanks.
    I had one question though, can you toggle flash on/off while you are showing the camera. Or if you could suggest how to set the flash property I can implement the button and toggle myself.
    Thanks again, cheers.

    1. codeonion says:

      Flash can be controlled once you allow permission

      In manifest,

      Then, you can try something like

      Camera cam;
      void ledon() {
      cam =;
      Parameters params = cam.getParameters();
      cam.autoFocus(new AutoFocusCallback() {
      public void onAutoFocus(boolean success, Camera camera) {

      void ledoff() {

      I hope it helps

      1. Dan says:

        Thanks a lot for your prompt reply.

        I understand the solution you provided. But I’m a little confused on how to implement it. Because the camera is accessed by mOpenCvCameraView which is an object of CameraBridgeViewBase class of OpenCV here and accessing the camera again using a different object (Camera cam in this case) to set flash would return camera already in use.
        Is there a way to set the parameters of flash and then pass the same camera object to the CameraBridgeViewBase class object?

        Sorry for the petty questions, but I am beginner with OpenCV and solving this will really help me a lot. Thanks again.

  15. Anonymous says:

    I am looking forward for taking pictures or saving picture. When you will cover this? Or can you help me a bit with this.

  16. Anonymous says:

    How do I take picture I can only see that my camera is started i cant capture image or snap a photo. Please help me on this.

    1. codeonion says:

      Thanks for your comment. I did not cover the “Taking pictures” and “Saving in a certain location” in this series yet. Nowadays, I am working on developing the app further. Follow me on twitter and facebook for updates 🙂

  17. jDark says:

    for the one who wants provide a full support with all the possible orientations (included reverse-portrait) can use this code below

    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    switch (mOpenCvCameraView.getDisplay().getRotation()) {
    case Surface.ROTATION_0: // Vertical portrait
    Core.transpose(mRgba, mRgbaT);
    Imgproc.resize(mRgbaT, mRgbaF, mRgbaF.size(), 0,0, 0);
    Core.flip(mRgbaF, mRgba, 1);
    case Surface.ROTATION_90: // 90° anti-clockwise
    case Surface.ROTATION_180: // Vertical anti-portrait
    Core.transpose(mRgba, mRgbaT);
    Imgproc.resize(mRgbaT, mRgbaF, mRgbaF.size(), 0,0, 0);
    Core.flip(mRgbaF, mRgba, 0);
    case Surface.ROTATION_270: // 90° clockwise
    Imgproc.resize(mRgba, mRgbaF, mRgbaF.size(), 0,0, 0);
    Core.flip(mRgbaF, mRgba, -1);
    return mRgba;

    don’t forgot to update manifest as well with


    1. codeonion says:

      Thank you very much for such a precious comment. You are doing a great work by contributing to the OpenCV and Android society 😀

  18. ND says:

    Hi there, like some of the other users, I get the following error;

    “It seems that you device does not support camera (or it is locked). Application will be closed.”

    You mentioned in one of your earlier replies that the problem could be in the Manifest file. I’m assuming your referring to AndroidManifest.xml? If so I have the following;

    I have also tried changing the conditions to true but I still the the “It seems that you device does not support camera (or it is locked). Application will be closed.” error message.

    Doesn’t seem to work for either true or false. Can you kindly help point me in the right direction? Thank you.

    1. ND says:

      For some reason the code snippet didn’t go through, this is what I have for permissions;

    2. Anonymous says:

      Try to find your app in the App section of Settings (or something equivalent to your phone) and choose Camera in the Permissions field of your app manually

    3. Anonymous says:

      protected void onCreate(Bundle savedInstanceState) {
      Log.i(TAG, “called onCreate”);


      mOpenCvCameraView = (JavaCameraView) findViewById(;
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);





  19. ND says:

    Hi there,

    I tried importing the following but when I type it out, it immediately disappears;

    import android.view.WindowManager;

    import org.opencv.core.Core;
    import org.opencv.core.CvType;
    import org.opencv.core.Mat;
    import org.opencv.imgproc.Imgproc;

    I was able to properly setup the OpenCV project in Android Studio. I’m using Android Studio 2.1.2 on Windows 10.

    1. ND says:

      I was able to rectify this by typing all the code in the final version first. Android Studio 2.1.2 automatically imports the corresponding Android and OpenCV classes.

      In my version I had to modify the onCameraFrame method to the following convention;

      public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

      // TODO Auto-generated method stub
      mRgba = inputFrame.rgba();
      // Rotate mRgba 90 degrees
      Core.transpose(mRgba, mRgbaT);
      Imgproc.resize(mRgbaT, mRgbaF, mRgbaF.size(), 0,0, 0);
      Core.flip(mRgbaF, mRgba, 1 );

      return mRgba; // This function must return


      update -> public MainActivity_show_camera() to public MainActivity() as per my naming convention.

      Hope this helps someone else.

  20. vishal says:

    is there any way we can run this app without downloading opencv manager

  21. Anonymous says:

    where can i find “” ? or i must add the new mainactivity again ? and how to do that ? please sir help me

    1. codeonion says:

      Thanks for the comment.

      First of all, it is up to you to rename the main activity. I renamed it to include a customization lesson in the tutorial 🙂

      Basically, if you wish, just rename or create a new activity with the name “”. Also, update Manifest file to reflect the change you made so the activity is loaded (in case if you are willing to make it the first activity that you are loading)

  22. TFBOYS says:

    During I run the program, it failed and give some print like this “Service Intent must be explicit: Intent { act=org.opencv.engine.BIND }”, Please help…

    1. codeonion says:

      Are you trying to make a service out of OpenCV 😀 ?

      Try putting SDK version to 19 or 21. But that may conflict with your project settings.

  23. Komal Shah says:

    i used ur code it is good, but i am unable to use gives me option for dowloading opencv manager.If i download it..code works perfectly otherwise not. what if i want to use without installing opencv manager in my device.

    1. codeonion says:

      Thanks for the comment :). It is a great idea.

      If that helps, then you can possibly minimize the size of OpenCV manager and provide it for download. I haven’t tried it myself, but you can modify the openCV manager by yourself here

      Or maybe remove the dependancy by adding the required assets in your own project 😀
      Think about creating a lightweight version of OpenCV Manager

      1. Cj says:

        Hey Mr codeonion!
        Thanks a lot for sharing this series of tutorials, it is excellent! 🙂

        I also think it is a great idea. It would be great a tutorial about how to include the functionality of the OpenCV Manager directly in apps. Something that explains the following link, but step by step for dummies…

        Why do you think it is said in that link that: “This approach is deprecated for the production code, release package is recommended to communicate with OpenCV Manager via the async initialization described above”?

        btw when are you planning to post the rest of the tutorials (Show a Box on screen, Apply basic color filters using OpenCV, etc) ?


  24. vishal says:

    hii.. thanks for the wonderful tutorial..
    well after the first run am getting a java.lang.nullponiter exception on line
    ” mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);”
    plse help

  25. Andy says:

    Nice tutorial and the orientation problem is fixed now. But my preview isn’t full screen, do you know how to handle the problem? Thanks!

  26. Efe says:

    Hi! I did everything right but in terms of sending it to a real device( i have samsung galaxy s3 ), i had to decrease the min api level to 16. And when i run the app it doesnt give an error but also nothing happens. canu tell me why? Thanks for the post btw

    1. codeonion says:

      Maybe the camera needs permission from your Manifest file.

  27. Efe says:

    Hi. Thanks for the post. I did everything correct and I am trying to use it with samsung galaxy s3. When I run the app ,it doesnt fail but also nothing happens too. I decreased my min api to level to 16 to let the app work on samsung s3. Does it effect the working of the app? Thank you

  28. Kimchheang says:

    I got this error.

    Camera-JNI: Callback buffer was too small! Expected 1382400 bytes, but got 608256 bytes!

    Please Help.!

  29. Dener says:

    I had this problem too. It happens because of Android Marshmallow’s new app permission system.
    You can grant permission manually under Settings > Apps > (your app’s name) > Permissions.

  30. Mas Wiwid says:

    Really good tutorial and easy to follow.
    how can I make it full screen ? and the app give me image with 90 degree orientaion to the left
    do you have any recomendation book should I read or site should I visit to learn opencv4android ?

    1. codeonion says:

      Thanks for the comment. I will change the post to make the camera full screen. Thanks for pointing out 🙂

      As for the books I highly recommend that you read these books:

      Learning OpenCV 3 (Publisher: O’Reilly Media)

      And this one specifically for Android and OpenCV

      Android Application Programming with OpenCV

    2. codeonion says:


      The orientation issue has been resolved in the code. Now the orientation is working perfectly in portrait mode. You can set it to other orientations too if you wish. Simply go through the tutorial again and you will see that it is now improved, simpler and cleaner.

      Let me know if the problem is solved.

      1. Andrew says:


        Thanks for the nice tutorials! 🙂
        But i have the full screen issue too, do you already know how to fix the problem?

  31. Håvard says:

    I get an error after running the app. “It seems that you device does not support camera (or is locked). Application will be closed.” I am running the app on a Nexus 6P with Android 6.0.1. Is this because camera is deprecated? What can I do to fix it?

    1. codeonion says:

      It could be the permission in Manifest file.

      Make sure that your app’s manifest file has the permission to use the camera feature of the phone.

  32. Alexander says:

    Thanks for making this tutorial 🙂 My camera is rotated 90 degrees to the left and stays this way even if I rotate my phone. Do you know how to set the camera orientation?

    1. mkshivaswami says:

      Did you find out how to solve that issue ?
      I am having the same trouble.

    2. Anonymous says:

      I have the same trouble!
      Did you know how to solve it?

      1. codeonion says:

        Hi. Thanks for the comment 🙂

        The orientation issue has been resolved in the code. Now the orientation is working perfectly in portrait mode. You can set it to other orientations too if you wish. Simply go through the tutorial again and you will see that it is now improved, simpler and cleaner.

        Let me know if the problem is solved.

    3. codeonion says:

      Hi. Thanks for the comment 🙂

      The 90 degrees orientation issue has been resolved. Now the orientation is working perfectly in portrait mode. You can set it to other orientations too if you wish. Simply go through the tutorial again and you will see that it is now improved, simpler and cleaner.

      Let me know if the problem is solved.

  33. Chi says:

    I use OpenCV 3 and Android Studio 2. Folowing the steps give, I am able to build and run successfully. I had to comment out two lines in
    This temporary fix is applied due to Android Studio not recognising “.design.”.
    Run using USB debugger on Samsung Galaxy S4 Android versioin 5.0.1. First launch this app the user is given the choice to install OpenCV from Apps Store. The correct “ARM” version is automatically located by the device. After installing, re-launch the app to observe that the camera feed is now shown on screen.

    1. codeonion says:

      Thanks for pointing out 🙂 Those two lines were not necessary in the project. But I refrained myself from editing/removing too much from the default new project code 😀

      I am reflecting the changes in the original post. Thanks 🙂

  34. mkshivaswami says:

    The following classes could not be found:
    – (Fix Build Path, Create Class)

    This is the error. Any help where I might have done wrong. ?

    1. codeonion says:

      Thanks for the comment.

      In this tutorial, the class is not used at all. But you can check if it is loaded properly. If you follow this tutorial, you will have the OpenCV libraries and classes loaded properly.

      Make sure that the exists in your project browser like this:

      Make sure that exists

      I will make use of this class in the next tutorials. Make sure you read 🙂


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.