29 Aralık 2014 Pazartesi

Undefined reference to symbol 'v4l2_munmap' error while installing OpenCV 2.4.9 on Ubuntu 14.04

If you are dealing with image processing in one of your projects, almost all roads lead to OpenCV and Tesseract. Tesseract is a OCR library created by HP and currently is handled by Google. In order to get good results in OCR process, you need to preprocess your image for OCR. For this, you can use OpenCV image processing library. Maybe installing OpenCV in various platforms might be very easy but installing it on Ubuntu is a living hell.

There are plenty of guides and resources about installing OpenCV on Ubuntu. I tried most of them but every time I got the same error: " Undefined reference to symbol 'v4l2_munmap' ". Before installing OpenCV, some other packages must be installed on Ubuntu which you can find everywhere. If you install required packages and still got the same error, there is a solution.

Possibly, you get the error message below:

/usr/bin/ld: ../../lib/libopencv_highgui.a(cap_libv4l.cpp.o): undefined reference to symbol 'v4l2_munmap'
//usr/lib/x86_64-linux-gnu/libv4l2.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make2: * [bin/opencv_test_highgui] Error 1
make1: [modules/highgui/CMakeFiles/opencv_test_highgui.dir/all] Error 2
make: ** [all] Error 2

To overcome this problem, all you need is to append " -lv4l2 " to [OPENCV_BUILD_DIR]/modules/highgui/CMakeFiles/opencv_test_highgui.dir/link.txt

After making the change, you need to execute make command again. Possibly, you will get the same error with different file names. All you need to do is applying same process to all files which gives the same error. For example if you see " modules/superres/CMakeFiles/opencv_test_superres.dir/all " near Error 2, you need to append " -lv4l2 " to [OPENCV_BUILD_DIR]/modules/superres/CMakeFiles/opencv_test_superres.dir/link.txt.

Unfortunately, you will get this error in more than 50 files. Of course more elegant solution is to detect these files and append " -lv4l2 " to all of them before starting make process.

By the way, if you want to compile OpenCV without bothering required packages or other things, there is a simple .sh file which handles all of these processes. You can download it from the link below and run the file to complete installation easily.


( opencv 2.4.9, tesseract, ubuntu 14.04, compile error, DSO missing from command line, -lv4l2 )
OpenCV installation script: https://github.com/jayrambhia/Install-OpenCV/tree/master/Ubuntu 
Reference: http://code.opencv.org/issues/3726

27 Kasım 2014 Perşembe

Custom TextView In Android With Styles

TextView is one of the most useful components in Android but at some point, it still cannot meet our requirements. For example, if you want to use a custom TypeFace (aka Font), you need to change it programmatically in the Activity like stated below:

Typeface typeFace = Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf");
TextView textView = (TextView) findViewById(R.id.myTextView);
textView.setTypeFace(typeFace);

Here, we've read our "Roboto-Regular.ttf" font file from "assets" folder and set it as the TypeFace of textView object. It is a good, simple, working solution but it is not feasible if you want to replace TypeFace for all of your TextViw components in the application. To set a custom font for all of your TextView components in your application, you can extend TextView class with your custom class. There are other methods like creating a custom component with its attributes using xml files but the solution below might be the best for you if you do not need too much feature but changing font family:

RobotoTextView.java
public class RobotoTextView extends TextView {
    public RobotoTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }
    public RobotoTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }
    public RobotoTextView(Context context) {
        super(context);
        initialize();
    }
    private void initialize() {
        if (!isInEditMode()) {
            Typeface currentTypeFace = getTypeface();
            int style = currentTypeFace.getStyle();
            Typeface type = setFontFamily(style);
            setTypeface(type);
        }
    }
    private Typeface setFontFamily(int style){
        Typeface type = null;
        if(style==Typeface.BOLD){
            type = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Bold.ttf");
        }else if(style==Typeface.ITALIC){
            type = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Italic.ttf");
        }else{
            type = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Regular.ttf");
        }
        return type;
    }
    @Override
    public void setTypeface(Typeface tf, int style) {
        Typeface type = setFontFamily(style);
        super.setTypeface(type, style);
    }
}

MyView.xml
<com.test.RobotoTextView     android:id="@+id/myTextView"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="My Custom TypeFace"
     android:textStyle="bold"/>

Here, I extend TextView class with my own custom class and override the necessary constructors with custom ones. One problem I faced during these processes was I cannot make the text bold, italic etc. To overcome this issue, you must find bold and italic versions of your font and put them in your assets folder also. Then you need to check the style attribute in your methods and retrieve the related .ttf file considering the style attribute (bold or italic). By doing this, you can easily use "android:textStyle" attribute in your custom TextView component.

( android, custom textview, typeface, style, bold, italic, andrpid:textStyle )