Is it possible to zoom and focus using OpenCV on Android?
Asked Answered
R

1

5

I am doing some image detection on Android with OpenCV. I noticed that the OpenCV camera is quite blurry if one tries to hold it up to a small object. Is there a way to zoom the camera and/or customize the mode of focus? I'm familiar with implementing touch focus on the regular android camera class but am not sure about what's possible with OpenCV's classes. I couldn't find much on this online.

Cheers, Kevin

Roseliaroselin answered 22/9, 2015 at 14:0 Comment(1)
Amazing solution by phani_rohith. Very thorough and clean. Thank you!Roseliaroselin
T
8

It is possible to zoom, create Zoomcameraview class and extend JavaCameraView. use Camera.Parameters to set zoom. For UI use seekBar

Zoomcameraview.java

package com.example.camerazoomopencv;

import android.content.Context;
import android.content.SharedPreferences;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;

import org.opencv.android.JavaCameraView;


public class Zoomcameraview extends JavaCameraView {
public Zoomcameraview(Context context, int cameraId) {
     super(context, cameraId);
}

public Zoomcameraview(Context context, AttributeSet attrs) {
    super(context, attrs);
}

protected SeekBar seekBar;

public void setZoomControl(SeekBar _seekBar)
{
    seekBar=_seekBar;
}

protected void enableZoomControls(Camera.Parameters params)
{

     final int maxZoom = params.getMaxZoom();
     seekBar.setMax(maxZoom);
     seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
         int progressvalue=0;
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
            progressvalue=progress;
            Camera.Parameters params = mCamera.getParameters();
            params.setZoom(progress);
            mCamera.setParameters(params);



        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }



     }

    );

}


protected boolean initializeCamera(int width, int height)
{

    boolean ret = super.initializeCamera(width, height);


    Camera.Parameters params = mCamera.getParameters();

    if(params.isZoomSupported())
        enableZoomControls(params);

    mCamera.setParameters(params);

    return ret;
}

}

MainActivity.java

package com.example.camerazoomopencv;

import java.io.IOException;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.core.Mat;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.widget.SeekBar;


public class MainActivity extends Activity implements CvCameraViewListener2     {


Zoomcameraview zoomcameraview;


private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
                zoomcameraview.enableView();
                break;
            default:
                super.onManagerConnected(status);
                break;
        }
    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    zoomcameraview = (Zoomcameraview)findViewById(R.id.ZoomCameraView);
    zoomcameraview.setVisibility(SurfaceView.VISIBLE);
    zoomcameraview.setZoomControl((SeekBar) findViewById(R.id.CameraZoomControls));
    zoomcameraview.setCvCameraViewListener(this);


}
@Override
public void onPause()
{
    super.onPause();
    if (zoomcameraview!= null)
        zoomcameraview.disableView();
}

public void onDestroy() {
    super.onDestroy();
    if (zoomcameraview != null)
        zoomcameraview.disableView();
}

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this,mLoaderCallback );
}

@Override
public void onCameraViewStarted(int width, int height) {
    // TODO Auto-generated method stub

}
@Override
public void onCameraViewStopped() {
    // TODO Auto-generated method stub

}
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    // TODO Auto-generated method stub
    final Mat rgba=inputFrame.rgba();
    return rgba;
}


}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:opencv="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.camerazoomopencv.MainActivity" >

<com.example.camerazoomopencv.Zoomcameraview
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="gone"
    android:id="@+id/ZoomCameraView"
    opencv:show_fps="false"
    opencv:camera_id="any"
    tools:context=".MainActivity"/>

<SeekBar
    android:layout_width="match_parent"
    android:paddingLeft="@dimen/seekbar_horizontal_margin"
    android:paddingRight="@dimen/seekbar_horizontal_margin"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/ZoomCameraView"
    android:layout_centerInParent="true"
    android:id="@+id/CameraZoomControls"/>

</RelativeLayout>
Teasel answered 12/10, 2015 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.