gpt4 book ai didi

android - 单击焦点创建 CameraSource - Android QR 代码检测

转载 作者:行者123 更新时间:2023-11-29 19:56:56 26 4
gpt4 key购买 nike

我正在开发一个 android 应用程序,它允许用户检查 QR 码内容并根据读取结果执行某些操作。

为了提高性能,我想实现两种方法:

  • onClickFocus(允许用户在点击屏幕时对焦相机)
  • 打开/关闭闪光灯(允许用户打开/关闭闪光灯)

我做了一些挖掘,发现要管理相机和闪光灯,我需要能够将 Camera 作为对象本身进行管理。

噩梦就此开始。

我正在使用以下代码来显示相机结果和跟踪二维码。

    import android.app.FragmentTransaction;
import android.content.Context;


import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
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.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import java.io.IOException;



public class MainReadActivity extends AppCompatActivity {


public SurfaceView cameraView;
private TextView barcodeInfo;
public BarcodeDetector barcodeDetector;
public CameraSource cameraSource;
public Vibrator v;
public String textInfo;
public DrawerLayout mDrawerLayout;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_read);
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_main);


getSupportFragmentManager().findFragmentById(R.id.drawer_layout_main);

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).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) {


new Runnable() { // Use the post method of the TextView
public void run() {


v.vibrate(500);
// textInfo = barcodes.valueAt(0).displayValue;

MyFragmentDialog newf = new MyFragmentDialog();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newf);

transaction.addToBackStack("tag");
transaction.commit();

}
};
}
}

});







}

public void onBackPressed() {
// do nothing
}
}

因此,我需要从 CameraSource 访问 Camera(对吗?!)一旦不行,我就尝试用这个CameraSource来自 GoogleSamples 的 git 的类允许使用 setFocusMode 方法...但不幸的是我没有成功。

我还尝试使用 API 21,因为 API 22 no longer supports相机和相机首选项。

我很确定这不仅是我的问题,而且找不到解决方法。

有人可以帮忙吗?

已修复:

只需使用这个 CameraSource (github.com/googlesamples/android-vision/blob/master/visionSamples/barcode-reader/app/src/main/java/com/google/android/gms/samples/vision/barcodereader/用户界面/相机/CameraSource.java)。是的,我知道,我已经建议了...但是这次我解决了我的问题!所以,如果你要使用它,请确保你的编译看起来像这样:

compile 'com.google.android.gms:play-services:8.1.0'

最佳答案

初始化这些并在OnCreate中定义它们

Camera.Parameters params;
Camera camera;
CameraSource cameraSource;
SurfaceView cameraView;
boolean isFlash = false;

调用changeFlashStatus()方法打开闪光灯,再次调用关闭闪光灯

public void changeFlashStatus() {
Field[] declaredFields = CameraSource.class.getDeclaredFields();

for (Field field : declaredFields) {
if (field.getType() == Camera.class) {
field.setAccessible(true);
try {
camera = (Camera) field.get(cameraSource);
if (camera != null) {
params = camera.getParameters();
if (!isFlash) {
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
flashImage.setColorFilter(getResources().getColor(R.color.yellow));
isFlash = true;
} else {
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
flashImage.setColorFilter(getResources().getColor(R.color.greyLight));
isFlash = false;
}
camera.setParameters(params);
}

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

break;
}
}
}

要让相机对焦,您需要一个特定区域 ( Rect ) 将其传递给相机以对焦该区域。所以我们必须为 surfaceView 实现 onTouchListener() 所以当我们触摸 surfaceView 时我们创建 MotionEvent 这是确定您触摸 surfaceView 的确切位置,然后我们可以从 MotionEvent 中提取 Rect

在您的 OnCreate 中调用 initCameraFocusListener()。摄像头启动后安全调用

private void initCameraFocusListener() {
cameraView.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {

cameraFocus(event, cameraSource, Camera.Parameters.FOCUS_MODE_AUTO);

return false;
}
});
}

private boolean cameraFocus(MotionEvent event, @NonNull CameraSource cameraSource, @NonNull String focusMode) {
Field[] declaredFields = CameraSource.class.getDeclaredFields();

int pointerId = event.getPointerId(0);
int pointerIndex = event.findPointerIndex(pointerId);
// Get the pointer's current position
float x = event.getX(pointerIndex);
float y = event.getY(pointerIndex);

float touchMajor = event.getTouchMajor();
float touchMinor = event.getTouchMinor();

Rect touchRect = new Rect((int)(x - touchMajor / 2), (int)(y - touchMinor / 2), (int)(x + touchMajor / 2), (int)(y + touchMinor / 2));

Rect focusArea = new Rect();

focusArea.set(touchRect.left * 2000 / cameraView.getWidth() - 1000,
touchRect.top * 2000 / cameraView.getHeight() - 1000,
touchRect.right * 2000 / cameraView.getWidth() - 1000,
touchRect.bottom * 2000 / cameraView.getHeight() - 1000);

// Submit focus area to camera

ArrayList<Camera.Area> focusAreas = new ArrayList<Camera.Area>();
focusAreas.add(new Camera.Area(focusArea, 1000));

for (Field field : declaredFields) {
if (field.getType() == Camera.class) {
field.setAccessible(true);
try {
camera = (Camera) field.get(cameraSource);
if (camera != null) {
params = camera.getParameters();
params.setFocusMode(focusMode);
params.setFocusAreas(focusAreas);
camera.setParameters(params);

// Start the autofocus operation

camera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean b, Camera camera) {
// currently set to auto-focus on single touch
}
});
return true;
}

return false;
} catch (IllegalAccessException e) {
e.printStackTrace();
}

break;
}
}
return false;
}

关于android - 单击焦点创建 CameraSource - Android QR 代码检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36669064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com