从片段读取的文本,相机没有打开

时间:2018-04-26 15:26:49

标签: android camera fragment google-vision

我正在片段中创建一个文本阅读器,它将使用原生相机和谷歌播放服务愿景。我已经在活动中实现并且工作正常。现在我正在尝试片段。但没有工作。相机没有打开。 以下是我的完整代码:请帮助。

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();

        }
    });

请指导我

0 个答案:

没有答案