我正在使用SurfaceView在Android中编写QR码阅读器。我在互联网上的教程中编写了这段代码,我对SurfaceView不是很熟悉。
我有一个主要问题,我不知道如何在SurfaceView中设置自动对焦。
我搜索了这么多。我尝试了一些我在互联网上找到的方法,但我无法设置自动对焦。我该如何实现它?
我的代码:
public class page_qr extends AppCompatActivity {
SurfaceView cameraView;
TextView barcodeInfo;
BarcodeDetector barcodeDetector;
CameraSource cameraSource;
static Context _C;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_qr);
_C = this;
focusMode = (TextView) findViewById(R.id.focus_mode);
cameraView = (SurfaceView) findViewById(R.id.camera_view);
barcodeInfo = (TextView) findViewById(R.id.code_info);
barcodeDetector =
new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();
cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedFps(30.0f)
.build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
cameraSource.start(cameraView.getHolder());
} catch (IOException ie) {
Log.e("CAMERA SOURCE", ie.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
barcodeDetector.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() { // Use the post method of the TextView
public void run() {
barcodeInfo.setText( // Update the TextView
barcodes.valueAt(0).displayValue
);
}
});
}
}
});
}
我的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="4">
<TextView
android:id="@+id/code_info"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:text="Nothing to read."
android:textColor="#DE043B3F"
android:textSize="20sp" />
<SurfaceView
android:id="@+id/camera_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_weight="3"
android:focusable="true"
android:focusableInTouchMode="true" />
</LinearLayout>
用这个
cameraSource = new CameraSource.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.setAutoFocusEnabled(true)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedFps(30.0f)
.build();
在下面的链接有一个很好的教程:http://technologysupportforall.blogspot.gr/2014/01/custom-camera-using-surfaceview-android.html
虽然教程没有提到它,但您应该在清单中添加以下行:
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
如果你不这样做,你的代码将无效。