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
- Edit Manifest
- Add permission to allow camera.
- Support various screen sizes and adaptability of camera.
- Use specific features of the camera.
- Add a custom Layout show_camera.xml to display camera in it.
- Edit MainActivity_show_camera.java
- Import required android classes
- Import OpenCV classes
- Connect to OpenCV manager
- Initiate LogCat to log events of out app
- SHOW CAMERA ON LAYOUT
- Corrected Orientation (portrait)
- Edit Manifest
Trust me, it is very simple to do. Lets do it!
Manifest.xml
We need to edit the AndroidManifest.xml file like following:
- Allow permission to use camera
<uses-permission android:name="android.permission.CAMERA"/>
- Allow camera to optimize to the dimensions of the device in use
<supports-screens android:resizeable="true" android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" />
- Use Front camera and autofocus of camera :S
<uses-feature android:name="android.hardware.camera" android:required="false"/> <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/> <uses-feature android:name="android.hardware.camera.front" android:required="false"/> <uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>
The Manifest.xml file in its entirety is given here
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="opencv.codeonion.com.opencv_test"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".MainActivity_show_camera" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <supports-screens android:resizeable="true" android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" /> <uses-permission android:name="android.permission.CAMERA"/> <uses-feature android:name="android.hardware.camera" android:required="false"/> <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/> <uses-feature android:name="android.hardware.camera.front" android:required="false"/> <uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/> </manifest>
This entire process is given here:
This is all you need to do in AndroidManifest.xml
Create a new layout (show_camera.xml )
Now in your project, do the following:
- Create a new layout called show_camera.xml
- Add the following code it to:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" xmlns:opencv="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent"> <org.opencv.android.JavaCameraView android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" android:id="@+id/show_camera_activity_java_surface_view" opencv:show_fps="true" opencv:camera_id="any" /> </LinearLayout>
Note that the JavaCameraView has the id of show_camera_activity_java_surface_view
This is given below:
MainActivity_show_camera.java
Import the following Android Classes in your MainActivity_show_camera:
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.MenuItem; import android.view.SurfaceView; import android.view.WindowManager;
Import the following OpenCV classes in your MainActivity_show_camera:
import org.opencv.android.JavaCameraView; import org.opencv.android.BaseLoaderCallback; import org.opencv.android.CameraBridgeViewBase; import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.imgproc.Imgproc;
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.
public class MainActivity extends AppCompatActivity implements CvCameraViewListener2 {
- Declare the following items in the class
// Used for logging success or failure messages private static final String TAG = "OCVSample::Activity"; // Loads camera view of OpenCV for us to use. This lets us see using OpenCV private CameraBridgeViewBase mOpenCvCameraView; // Used in Camera selection from menu (when implemented) private boolean mIsJavaCamera = true; private MenuItem mItemSwitchCamera = null; // These variables are used (at the moment) to fix camera orientation from 270degree to 0degree Mat mRgba; Mat mRgbaF; Mat mRgbaT;
- Now, lets call OpenCV manager to help our app communicate with android phone to make OpenCV work
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { @Override public void onManagerConnected(int status) { switch (status) { case LoaderCallbackInterface.SUCCESS: { Log.i(TAG, "OpenCV loaded successfully"); mOpenCvCameraView.enableView(); } break; default: { super.onManagerConnected(status); } break; } } };
- 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.
- CameraBridgeViewBase mOpenCvCameraView
- Initiate the LogCat
public MainActivity_show_camera() { Log.i(TAG, "Instantiated new " + this.getClass()); }
- Now, when the activity is created, display the OpenCV camera in the layout. show_camera.xml.
@Override protected void onCreate(Bundle savedInstanceState) { Log.i(TAG, "called onCreate"); super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setContentView(R.layout.show_camera); mOpenCvCameraView = (JavaCameraView) findViewById(R.id.show_camera_activity_java_surface_view); mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE); mOpenCvCameraView.setCvCameraViewListener(this); }
- The following three functions handle the events when the app is Paused, Resumed and Closed/Destroyed
@Override public void onPause() { super.onPause(); if (mOpenCvCameraView != null) mOpenCvCameraView.disableView(); } @Override public void onResume() { super.onResume(); if (!OpenCVLoader.initDebug()) { Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization"); OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback); } else { Log.d(TAG, "OpenCV library found inside package. Using it!"); mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); } } public void onDestroy() { super.onDestroy(); if (mOpenCvCameraView != null) mOpenCvCameraView.disableView(); }
- Now, we will do two main things:
- Receive Image Data when the camera preview starts on your screen 😀
public void onCameraViewStarted(int width, int height) { mRgba = new Mat(height, width, CvType.CV_8UC4); mRgbaF = new Mat(height, width, CvType.CV_8UC4); mRgbaT = new Mat(width, width, CvType.CV_8UC4); }
- Destroy image data when you stop camera preview on your phone screen
public void onCameraViewStopped() { mRgba.release(); }
- 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!
public Mat onCameraFrame(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 }
- Receive Image Data when the camera preview starts on your screen 😀
The entire code is given below:
package opencv.codeonion.com.opencv_test; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.MenuItem; import android.view.SurfaceView; import android.view.WindowManager; import org.opencv.android.JavaCameraView; import org.opencv.android.BaseLoaderCallback; import org.opencv.android.CameraBridgeViewBase; import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.imgproc.Imgproc; // OpenCV Classes public class MainActivity_show_camera extends AppCompatActivity implements CvCameraViewListener2 { // Used for logging success or failure messages private static final String TAG = "OCVSample::Activity"; // Loads camera view of OpenCV for us to use. This lets us see using OpenCV private CameraBridgeViewBase mOpenCvCameraView; // Used in Camera selection from menu (when implemented) private boolean mIsJavaCamera = true; private MenuItem mItemSwitchCamera = null; // These variables are used (at the moment) to fix camera orientation from 270degree to 0degree Mat mRgba; Mat mRgbaF; Mat mRgbaT; private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { @Override public void onManagerConnected(int status) { switch (status) { case LoaderCallbackInterface.SUCCESS: { Log.i(TAG, "OpenCV loaded successfully"); mOpenCvCameraView.enableView(); } break; default: { super.onManagerConnected(status); } break; } } }; public MainActivity_show_camera() { Log.i(TAG, "Instantiated new " + this.getClass()); } @Override protected void onCreate(Bundle savedInstanceState) { Log.i(TAG, "called onCreate"); super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setContentView(R.layout.show_camera); mOpenCvCameraView = (JavaCameraView) findViewById(R.id.show_camera_activity_java_surface_view); mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE); mOpenCvCameraView.setCvCameraViewListener(this); } @Override public void onPause() { super.onPause(); if (mOpenCvCameraView != null) mOpenCvCameraView.disableView(); } @Override public void onResume() { super.onResume(); if (!OpenCVLoader.initDebug()) { Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization"); OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback); } else { Log.d(TAG, "OpenCV library found inside package. Using it!"); mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); } } public void onDestroy() { super.onDestroy(); if (mOpenCvCameraView != null) mOpenCvCameraView.disableView(); } public void onCameraViewStarted(int width, int height) { mRgba = new Mat(height, width, CvType.CV_8UC4); mRgbaF = new Mat(height, width, CvType.CV_8UC4); mRgbaT = new Mat(width, width, CvType.CV_8UC4); } public void onCameraViewStopped() { mRgba.release(); } public Mat onCameraFrame(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 } }
The entire process of editing MainActivity_show_camera.java is shown here:
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.
65 responses to “Use OpenCV to show camera on android App with correct orientation”
setContentView(R.layout.show_camera);
mOpenCvCameraView = (JavaCameraView) findViewById(R.id.show_camera_activity_java_surface_view);
O que é esse R
Thank you!!! this help me a lot!!!
Thanks for nice tutorial.But,I want to know how to run my projects without installing openCV Manager.
Can someone explain, why we are adding 3 objects with type “Mat”, and how exactly we use it when rotate the camera?
Plzzzzz
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!
Why is the ‘this’ in mOpenCvCameraView.setCvCameraViewListener(this); underlined in red?
Very nice work. step by step guide helps alot thank you so much
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.
Hi,
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.
Hi,
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 🙂
Well Chiara, I never test my apps on Emulator, BUT!
Try this:
1. Open Emulator/OpenCV-2.4.3.2-android-sdk/apk/OpenCV_2.4.3.2_Manager.apk
2. Go to terminal
3. Go to go to your SDK tool/ directory
4. Run this command : adb install
Source: http://answers.opencv.org/question/5854/installing-opencv-manager-on-a-emulator/
I hope you find this helpful.
What about switching to front camera? I can switch with this but image is then upside down.
How do you do this?
Why does it crashes when i try to run on my phone? I did everything
It says Unfortunately, OpenCV_test has stopped.
FATAL EXCEPTION: main
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 }
I changed targetSdkVersion to 19 and it opens. However, it says OpenCV was not initialized correctly. Application will be shut down.
Have you installed OpenCV manager in your Android Device?
Hello!
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?
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?
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.
Flash can be controlled once you allow permission
In manifest,
Then, you can try something like
Camera cam;
void ledon() {
cam = Camera.open();
Parameters params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
cam.setParameters(params);
cam.startPreview();
cam.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
}
});
}
void ledoff() {
cam.stopPreview();
cam.release();
}
I hope it helps
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.
I am looking forward for taking pictures or saving picture. When you will cover this? Or can you help me a bit with this.
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.
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 🙂
for the one who wants provide a full support with all the possible orientations (included reverse-portrait) can use this code below
@Override
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);
break;
case Surface.ROTATION_90: // 90° anti-clockwise
break;
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);
break;
case Surface.ROTATION_270: // 90° clockwise
Imgproc.resize(mRgba, mRgbaF, mRgbaF.size(), 0,0, 0);
Core.flip(mRgbaF, mRgba, -1);
break;
default:
}
return mRgba;
}
don’t forgot to update manifest as well with
android:screenOrientation=”fullSensor”
android:configChanges=”keyboardHidden|orientation|screenSize”
Thank you very much for such a precious comment. You are doing a great work by contributing to the OpenCV and Android society 😀
Perfect! Thanks!!!
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.
For some reason the code snippet didn’t go through, this is what I have for permissions;
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
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, “called onCreate”);
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.show_camera);
mOpenCvCameraView = (JavaCameraView) findViewById(R.id.show_camera_activity_java_surface_view);
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);
}
}
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
{
}
}
Hi there,
I tried importing the following but when I type it out, it immediately disappears;
import android.view.WindowManager;
import org.opencv.android.JavaCameraView;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
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.
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
}
and
update -> public MainActivity_show_camera() to public MainActivity() as per my naming convention.
Hope this helps someone else.
is there any way we can run this app without downloading opencv manager
where can i find “MainActivity_show_camera.java” ? or i must add the new mainactivity again ? and how to do that ? please sir help me
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 “MainActivity_show_camera.java”. 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)
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…
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.
hello..
i used ur code it is good, but i am unable to use camera..it gives me option for dowloading opencv manager.If i download it..code works perfectly otherwise not. what if i want to use org.opencv.android.JavaCameraView without installing opencv manager in my device.
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 https://github.com/Itseez/opencv/blob/master/platforms/android/service/engine/src/org/opencv/engine/manager/ManagerActivity.java
Or maybe remove the dependancy by adding the required assets in your own project 😀
Think about creating a lightweight version of OpenCV Manager
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…
http://docs.opencv.org/2.4/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#application-development-with-static-initialization
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) ?
Cheers!
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
Hi!
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!
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
Maybe the camera needs permission from your Manifest file.
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
I got this error.
Camera-JNI: Callback buffer was too small! Expected 1382400 bytes, but got 608256 bytes!
Please Help.!
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.
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 ?
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)
http://shop.oreilly.com/product/0636920044765.do
And this one specifically for Android and OpenCV
Android Application Programming with OpenCV
http://shop.oreilly.com/product/9781849695206.do
Hi.
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.
Hi!
Thanks for the nice tutorials! 🙂
But i have the full screen issue too, do you already know how to fix the problem?
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?
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.
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?
Did you find out how to solve that issue ?
I am having the same trouble.
I have the same trouble!
Did you know how to solve it?
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.
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.
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 MainActivity.java
//import android.support.design.widget.FloatingActionButton;
//import android.support.design.widget.Snackbar;
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.
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 🙂
The following classes could not be found:
– org.opencv.android.JavaCameraView (Fix Build Path, Create Class)
This is the error. Any help where I might have done wrong. ?
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 javaCameraView.java exists in your project browser like this:
I will make use of this class in the next tutorials. Make sure you read 🙂
Thanks