Я создаю фрагмент текста для чтения, который будет использовать встроенную камеру и сервис Google Play. Я реализовал это в деятельности и работает нормально. сейчас я пытаюсь фрагментарно. но не работает. камера не открывается.
ниже мой полный код: пожалуйста, помогите.
package com.......;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
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;
/**
* A simple {@link Fragment} subclass.
*/
public class ApplyCardFragment extends Fragment {
TextView applyCard, changeInfo, changePhoto;
public ApplyCardFragment() {
// Required empty public constructor
}
SurfaceView cameraView;
TextView textView;
CameraSource cameraSource;
final int RequestCameraPermissionId = 1001;
@Override
public void onRequestPermissionsResult(int RequestCode, @NonNull String[] permission, int[] grantResults)
{
switch (RequestCode) {
case RequestCameraPermissionId:
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
return;
}
try {
cameraSource.start(cameraView.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
protected String FindText(String text, String findText, int number)
{
String found = "";
if(text.contains(findText))
{
int pos = text.indexOf(findText);
found =text.substring(pos,pos + number);
}
return found;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//return inflater.inflate(R.layout.fragment_apply_card, container, false);
View vw=inflater.inflate(R.layout.fragment_apply_card, container, false);
cameraView = (SurfaceView) vw.findViewById(R.id.surface_view);
textView = (TextView) vw.findViewById(R.id.text_view);
TextRecognizer textRecognizer = new TextRecognizer.Builder(getContext()).build();
if (!textRecognizer.isOperational())
{
//Log.w("Detector dependency is not avialable.");
}
else
{
cameraSource = new CameraSource.Builder(getContext(), textRecognizer)
.setFacing(cameraSource.CAMERA_FACING_BACK)
.setRequestedPreviewSize(1280, 1024)
.setRequestedFps(2.0f)
.setAutoFocusEnabled(true)
.build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, RequestCameraPermissionId);
return;
}
cameraSource.start(cameraView.getHolder());
}
catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
cameraSource.stop();
}
});
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");
//FindText();
textView.setText(FindText(item.getValue(),"SDC-",8));
}
// below code is for getting all string
//textView.setText(stringBuilder.toString());
}
});
}
}
});
}
return vw;
}
}
XML - это
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LayoutTop"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/text_view"
android:text="No Text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@android:color/white"
android:textSize="20sp"
/>
</LinearLayout>
После отладки я обнаружил: блок кода ниже не выполняется:
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, RequestCameraPermissionId);
return;
}
cameraSource.start(cameraView.getHolder());
}
catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
cameraSource.stop();
}
});
Пожалуйста, ведите меня