提问人:Clint William Theron 提问时间:11/15/2023 最后编辑:Clint William Theron 更新时间:11/15/2023 访问量:10
如何使用Mobile Vision API打开/关闭闪光灯?
How to turn Flash Light On/Off using Mobile Vision API?
问:
我正在使用移动视觉来识别图像上的文本。这几乎是我的MainActivity:
package com.usama.textrecognitionbycamera;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
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.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.text.TextBlock;
import com.google.android.gms.vision.text.TextRecognizer;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
// welcome ..
SurfaceView cameraView;
TextView textView;
CameraSource cameraSource;
final int RequestCameraPermission = 1001;
// 3
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case RequestCameraPermission: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
return;
}
try {
cameraSource.start(cameraView.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraView = findViewById(R.id.surface_view);
textView = findViewById(R.id.text_view);
// 1
final TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
if (!textRecognizer.isOperational()) {
Log.w("MainActivity", "Detected dependence are not found ");
} else {
cameraSource = new CameraSource.Builder(getApplicationContext(), textRecognizer)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedPreviewSize(1280, 1024)
.setRequestedFps(2.0f)
.setAutoFocusEnabled(true)
.build();
// 2
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (ActivityCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CAMERA},
RequestCameraPermission);
}
cameraSource.start(cameraView.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
// 4
textRecognizer.setProcessor(new Detector.Processor<TextBlock>() {
@Override
public void release() {
}
@Override
public void receiveDetections(Detector.Detections<TextBlock> detections) {
final SparseArray<TextBlock> items = detections.getDetectedItems();
if (items.size() != 0 ){
textView.post(new Runnable() {
@Override
public void run() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0 ;i < items.size();i++){
TextBlock item = items.valueAt(i);
stringBuilder.append(item.getValue());
stringBuilder.append("\n");
}
textView.setText(stringBuilder.toString());
Log.d("Text",stringBuilder.toString());
}
});
}
}
});
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textColor="#CE3030"
android:textSize="24sp"
android:text="no text"/>
</RelativeLayout>
我在清单中请求相机权限,这是视觉库实现:
implementation 'com.google.android.gms:play-services-vision:19.0.0'
现在,文档没有提到任何关于使用闪光灯的信息,所以我尝试使用相机管理器即兴创作:
android.hardware.camera2.CameraManager
我在onCreate中初始化相机管理器:
mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
mCameraId = mCameraManager.getCameraIdList()[0];
} catch (CameraAccessException e) {
e.printStackTrace();
}
并在 surfaceCreated 的 cameraView.getHolder() 回调中打开闪光灯,如下所示
try {
mCameraManager.setTorchMode(mCameraId, true);
} catch (CameraAccessException e) {
e.printStackTrace();
}
而且,你猜怎么着,我打开了闪光灯,但现在我无法关闭它。我尝试使用setTorchMode(mCameraId,false),但没有运气。我希望它在打开和关闭之间切换。我猜我有两个连接到相机源的连接(CameraSource 和 CameraManager),所以我能够打开它,但不能关闭它,因为视觉移动 api 正在使用它。
这里有什么解决方法吗?
答:
0赞
Clint William Theron
11/20/2023
#1
这实际上很简单。只需停止摄像机源并重新启动它。有关详细信息,请参阅下面的代码:
private void startTorch() {
// torch, from cmera2, needs to start with scanner camera
if (ActivityCompat.checkSelfPermission(
getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA},
RequestCameraPermission);
}
try {
cameraSource.stop();
mCameraManager.setTorchMode(mCameraId, true);
isTorchOn = true;
cameraSource.start(cameraView.getHolder());
} catch (CameraAccessException e) {
helper.showAlert(this, "Starting Camera Error", e.getMessage());
} catch (IOException ex) {
helper.showAlert(this, "Starting Camera Error", ex.getMessage());
}
}
private void stopTorch() {
if (ActivityCompat.checkSelfPermission(
getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA},
RequestCameraPermission);
}
try {
cameraSource.stop();
mCameraManager.setTorchMode(mCameraId, false);
isTorchOn = false;
cameraSource.start(cameraView.getHolder());
} catch (CameraAccessException e) {
helper.showAlert(this, "Starting Camera Error", e.getMessage());
} catch (IOException ex) {
helper.showAlert(this, "Starting Camera Error", ex.getMessage());
}
}
评论