我正在开发一个Android应用程序,该应用程序允许用户检查QR Code内容并根据读取结果执行某些操作。
为了提高性能,我想实现2种方法:
onClickFocus(允许用户在单击屏幕时聚焦相机)
打开/关闭闪光灯(允许用户打开/关闭闪光灯)
我做了一些挖掘,发现对于管理相机和闪光灯,我需要能够将Camera
作为对象本身进行管理。
这就是噩梦开始的地方。
我正在使用以下代码来显示相机结果并跟踪QR码。
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(对吗?!)
一旦不可能,我尝试使用GoogleSamples的git中的CameraSource类,该类允许使用setFocusMode方法...但是不幸的是,我没有成功。
我还尝试使用API 21,因为API 22 no longer supports Camera和CameraPreferences。
我很确定这不仅是我的问题,而且找不到解决它的方法。
有人可以帮忙吗?
固定:
只需使用此CameraSource(github.com/googlesamples/android-vision/blob/master/visionSamples/barcode-reader/app/src/main/java/com/google/android/gms/samples/vision/barcodereader/ui/camera /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
)将其传递给相机以对该区域进行聚焦。因此,我们必须为onTouchListener()
实现surfaceView
,因此当我们触摸surfaceView
时,我们会创建MotionEvent
来确定您确切地触摸surfaceView
的位置,然后可以从Rect
中提取MotionEvent
。在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;
}