5

我正在使用 OpenCV 在 Android 上进行一些图像检测。我注意到,如果试图将 OpenCV 相机放在一个小物体上,它会非常模糊。有没有办法缩放相机和/或自定义对焦模式?我熟悉在常规 android 相机类上实现触摸焦点,但不确定 OpenCV 的类有什么可能。我在网上找不到太多关于此的内容。

干杯,凯文

4

1 回答 1

8

可以缩放、创建 Zoomcameraview 类和扩展 JavaCameraView。使用 Camera.Parameters 设置缩放。对于 UI 使用 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>
于 2015-10-12T08:40:57.203 回答