Android自动对焦和图像预览尺寸

时间:2016-11-08 16:52:05

标签: android android-camera autofocus

我正在使用这两个来源制作QR码扫描仪

QR scanner android app

Fix the autofocus issue

我尝试修复自动对焦问题,但收到的错误是无法解析符号' Field' 此外,我尝试使用setRequestedPreviewSize方法解决相机预览的尺寸,但无法找到解决方案。

  

MainActivity.java

import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;

import java.io.IOException;
import java.util.List;

public class MainActivity extends Activity {

private SurfaceView cameraView;
private TextView barcodeInfo;
private CameraSource cameraSource;
private SurfaceHolder surfaceHolder;
private GoogleApiClient client;

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

    cameraView = (SurfaceView) findViewById(R.id.cameraView);
    barcodeInfo = (TextView) findViewById(R.id.infoTextView);

    BarcodeDetector detector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
    cameraSource = new CameraSource.Builder(this, detector).setRequestedPreviewSize(640, 480).build();

    cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                cameraSource.start(cameraView.getHolder());
            } catch (IOException e) {
                Log.e("CAMERA SOURCE", e.getMessage());
            }
        }

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

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            cameraSource.stop();
        }
    });

    detector.setProcessor(new Detector.Processor<Barcode>() {
        @Override
        public void release() {

        }

        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> barcodes = detections.getDetectedItems();
            if (barcodes.size() != 0) {
                barcodeInfo.post(new Runnable() {
                    @Override
                    public void run() {
                        barcodeInfo.setText(barcodes.valueAt(0).displayValue);
                    }
                });
            }
        }
    });

public void previewCamera() {

    try {
        cameraSource.setPreviewDisplay(surfaceHolder);

    } catch (IOException e) {
        e.printStackTrace();
    }


    CameraSource.Parameters param = cameraSource.getParameters();
    List<CameraSource.Size> sizeList = cameraSource.getParameters().getSupportedPreviewSizes();
    CameraSource.Size bestSize = sizeList.get(0);
    for (int i = 1; i < sizeList.size(); i++) {
        if ((sizeList.get(i).width * sizeList.get(i).height) > (bestSize.width * bestSize.height)) {
            bestSize = sizeList.get(i);
        }
    }

    List<Integer> supportedPreviewFormats = param.getSupportedPreviewFormats();
    for (Integer previewFormat : supportedPreviewFormats) {
        if (previewFormat == ImageFormat.YV12) {
            param.setPreviewFormat(previewFormat);
        }
    }

    PackageManager packageManager = getPackageManager();

    if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA) && packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS)) {

        param.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    }

    param.setPictureSize(bestSize.width, bestSize.height);
    param.setPreviewSize(bestSize.width, bestSize.height);

    cameraSource.setParameters(param);
    cameraSource.startPreview();
    previewing = true;

}
}
}
  

activity_main.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"
tools:context="com.example.android.diverseycare.MainActivity"
android:background="#ffffff"
android:padding="16dp">

<SurfaceView
    android:id="@+id/cameraView"
    android:layout_width="640px"
    android:layout_height="480px"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="67dp" />

<TextView
    android:id="@+id/infoTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:text="Nothing to read"
    android:textSize="20sp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

 </RelativeLayout>

我试图将代码组合起来使用注释中指出的代码,但是我错过了什么?对这些问题的良好资源的任何指示都非常感谢。 (这是我对anbdroid的第一次尝试,所以如果这是一个愚蠢的错误,请进一步尝试)

2 个答案:

答案 0 :(得分:0)

尝试使用此实用程序:https://github.com/dlazaro66/QRCodeReaderView  ....

答案 1 :(得分:0)

这就是我做的事情

  public void previewCamera() {

    try {
        camera.setPreviewDisplay(surfaceHolder);

    } catch (IOException e) {
        e.printStackTrace();
    }


    Camera.Parameters param = camera.getParameters();
    List<Camera.Size> sizeList = camera.getParameters().getSupportedPreviewSizes();
    Camera.Size bestSize = sizeList.get(0);
    for (int i = 1; i < sizeList.size(); i++) {
        if ((sizeList.get(i).width * sizeList.get(i).height) > (bestSize.width * bestSize.height)) {
            bestSize = sizeList.get(i);
        }
    }

    List<Integer> supportedPreviewFormats = param.getSupportedPreviewFormats();
    for (Integer previewFormat : supportedPreviewFormats) {
        if (previewFormat == ImageFormat.YV12) {
            param.setPreviewFormat(previewFormat);
        }
    }

    PackageManager packageManager = getPackageManager();

    if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA) && packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS)) {

        param.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    }




    param.setPictureSize(bestSize.width, bestSize.height);
    param.setPreviewSize(bestSize.width, bestSize.height);

    camera.setParameters(param);
    camera.startPreview();
    previewing = true;

}

希望它有所帮助!