如何将自动对焦添加到曲面视图?

时间:2016-07-22 05:34:16

标签: android

我正在开发一个QR解码器应用程序,我在其中创建了一个SurfaceView来显示摄像头输出,并ImageButton从图库中选择一个图像。 我正在使用谷歌播放服务来解码QR码,这需要使用表面视图来使用相机扫描QR。但它并没有专注于自己。 下面是相同的代码。

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;

public class Scanner extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanner);

        final SurfaceView cameraView = (SurfaceView) findViewById(R.id.camera_view);
        BarcodeDetector barcodeDectector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
        final CameraSource cameraSource = new CameraSource.Builder(this,barcodeDectector).setRequestedPreviewSize(1920,1080).build();
        cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                try {
                    cameraSource.start(cameraView.getHolder());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                cameraSource.stop();
            }
        });
        barcodeDectector.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {

            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections) {
                final SparseArray<Barcode> barcodes = detections.getDetectedItems();

                //QRcode found hence move to intent action.
                if( barcodes.size() != 0){
                    Intent i = new Intent(Scanner.this,Verify.class);
                    i.putExtra("xml",barcodes.valueAt(0).displayValue);
                    startActivity(i);
                }
            }
        });

        ImageButton scanImage = (ImageButton) findViewById(R.id.scanImage);
        scanImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //button action to be added
            }      

        });

    }
}

activity_scanner.xml如下。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"">

    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="400dip"
        android:layout_weight="70"
        android:id="@+id/camera_view"
        android:layout_alignParentLeft="true"
        />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/scanImage"
        android:layout_below="@+id/camera_view"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:src="@drawable/ic_image_black_48dp"
        android:layout_alignRight="@+id/camera_view"
        android:layout_alignEnd="@+id/camera_view" />

</RelativeLayout>

5 个答案:

答案 0 :(得分:1)

试试这个:

cameraView.requestFocus();

答案 1 :(得分:1)

cameraView.setFocusableInTouchMode(true);
cameraView.setFocusable(true);
cameraView.requestFocus();

答案 2 :(得分:1)

尝试像这样扩展Surface视图,并setFocusable(true)

public class CameraView extends SurfaceView implements SurfaceHolder.Callback {

    public CameraView(Context context) {
        super(context);
        getHolder().addCallback(this);
        setFocusable(true);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
}

编辑1: 然后在布局中你可以像这样使用这个类,

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://s...content-available-to-author-only...d.com/apk/res/android"
    xmlns:tools="http://s...content-available-to-author-only...d.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

<com.abhayjatindoshi.encryptorcode.qrlogin.javaclass.CameraView
    android:layout_width="match_parent"
    android:layout_height="400dip"
    android:layout_weight="70"
    android:id="@+id/camera_view"
    android:layout_alignParentLeft="true"
    />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/scanImage"
    android:layout_below="@+id/camera_view"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:src="@drawable/ic_image_black_48dp"
    android:layout_alignRight="@+id/camera_view"
    android:layout_alignEnd="@+id/camera_view" />

</RelativeLayout>

使用如上所述的布局。

答案 3 :(得分:0)

上面并没有解决我的“焦点”问题,但我找到了一个帮助我的另一个解决方案:

private void setCamFocusMode(){
if(null == mCamera) {
    return;
 }

/* Set Auto focus */ 
Parameters parameters = mCamera.getParameters();
List<String>    focusModes = parameters.getSupportedFocusModes();
if(focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)){
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);   
 } else 
 if(focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)){
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
 }   

 mCamera.setParameters(parameters);
}

oryginally发布在这里: https://stackoverflow.com/a/27092999/6225516

答案 4 :(得分:0)

如果仍然有人在寻找它,我已经通过在以下行的.setAutoFocusEnabled(true)之前添加.build来解决了这个问题:

BarcodeDetector barcodeDectector = new BarcodeDetector.Builder(this)
    .setBarcodeFormats(Barcode.QR_CODE)
    .build();*