Java Question Wrapping lib error: onActivityResult: wi is null

Discussion in 'Libraries developers questions' started by laomms, Jul 31, 2018.

  1. laomms

    laomms Member Licensed User

    I reference Johan Schoeman and DonManfred's sample want wrpping a github google vision source code .
    evething is well, I use SLC1.06 wrpping the lib successful.

    but when inport the b4a, error occurred: onActivityResult: wi is null
     

    Attached Files:

  2. Johan Schoeman

    Johan Schoeman Expert Licensed User

    The is probably something missing in your Manifest file.
     
  3. laomms

    laomms Member Licensed User

    I think unrelated manifest file, I have modify the manifest many times, still the same
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    This means that the IOnActivityResult object was released before the activity was resumed. Make sure to make it a global variable.
     
  5. laomms

    laomms Member Licensed User

    I have no idea about this: onActivityResult: wi is null
    IOnActivityResult always released before the activity was resumed.

    onActivityResult: wi is null
    ** Activity (main) Resume **
    ** Activity (main) Pause, UserClosed = true **
    *** Service (starter) Create ***
    ** Service (starter) Start **
    ** Activity (main) Create, isFirst = true **
    ** Activity (main) Resume **
    ** Activity (main) Pause, UserClosed = false **
     
  6. Johan Schoeman

    Johan Schoeman Expert Licensed User

    I will PM you over the weekend. I have got it working (shortcut wrap) :)

    1.png
     
    laomms likes this.
  7. laomms

    laomms Member Licensed User

    really, johan, great thanks!
     
  8. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Here it is - attached:
    1. Extract the B4A project from b4aTra.zip
    2. Unzip resource.zip and copy the resource folder and its contents to be on the same folder level as that of the /Objects and /Files folders of the B4A project
    3. Unzip DemoRes.zip and copy the DemoRes folder and its contents to be on the same folder level as that of the /Objects and /Files folders of the B4A project
    4. Extract the B4A lib files from b4aLibFiles.zip and copy the jar and xml to your additional library folder

    I have also attached the Java code (src folder). Seeing that you have already managed to compile your project I assume you know how and where to add the "libs" folder with the necessary jars in order to compile the Java project with SLC.

    I have done nothing special in the wrapper (such as raising events, etc) other than adding the ability to kick start the original project (i.e only a short cut wrap) from within a B4A project. You can modify it as you wish but the "onActivityResult: wi is null" error in not there...;)

    Take note of the B4A project's Manifest file.

    Enjoy!

    JS
     

    Attached Files:

    Last edited: Aug 3, 2018
  9. laomms

    laomms Member Licensed User

    thanks very much !!!!!

    123.png
     

    Attached Files:

    Last edited: Aug 4, 2018
    Johan Schoeman likes this.
  10. laomms

    laomms Member Licensed User

    update to landscape only when Ocr, these is some piece source:

    Code:
    #Region  Project Attributes
        
    #ApplicationLabel: b4aTRA
        
    #VersionCode: 1
        
    #VersionName:
        
    'SupportedOrientations possible values: unspecified, landscape or portrait.
        #SupportedOrientations: portrait
        
    #CanInstallToExternalStorage: False
    #End Region

    #AdditionalRes: ..\res
    #AdditionalRes: ..\resource

    #Region  Activity Attributes
        
    #FullScreen: False
        
    #IncludeTitle: True
    #End Region

    Sub Process_Globals


    End Sub

    Sub Globals
     
        
    Dim scan As OcrLib

        
    Private Button1 As Button
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    Activity.LoadLayout("main")
     
        scan.Initialize(
    "scan")

    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub


    Sub Button1_Click
     
        scan.startDetection
     
    End Sub

    Sub scan_scanned_text(scannedText As String)
     
        
    Log("Scanned text = " & scannedText)

     
    End Sub
    Manifast shoud add
    android:configChanges="keyboardHidden|orientation:
    Code:
    <activity android:name="com.tuts.prakash.simpleocr.MainActivity"
                      android:configChanges=
    "keyboardHidden|orientation">
            </
    activity>
    activity_main.xml modify:
    Code:
    android:orientation="horizontal"
        android:screenOrientation=
    "landscape"

    MainActivity.java add
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    Code:
    public class MainActivity extends AppCompatActivity  {

        
    private static final String TAG = "MainActivity";
        static AtomicBoolean surfaceExists = new AtomicBoolean(
    false);
        
    private static final int CAMERA_REQUEST_ID = 2122;
        
    public static final String mTextBlock = "String";


        
    private SurfaceHolder mSurfaceHolder;

        
    Camera mCamera;
        Context mContext;
        SurfaceView mCameraView;
        
    TextView mTextView;
        CameraSource mCameraSource;
        CameraInfo mCameraInfo;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            mTextView = (
    TextView)findViewById(R.id.text_view);
            mCameraView = (SurfaceView)findViewById(R.id.surfaceView);
       

            startCameraSource();
        
    }
     
     

        private void startCameraSource() {
            TextRecognizer textRecognizer = new TextRecognizer.Builder(this).build();
            if (textRecognizer.isOperational()) {
                mCameraSource = new CameraSource.Builder(this, textRecognizer)
                        .setFacing(CameraSource.CAMERA_FACING_BACK)
                        .setRequestedPreviewSize(1280, 1024)
                        .setRequestedFps(2.0f)
                        .setAutoFocusEnabled(true)
                        .build();
                mCameraView.getHolder().addCallback(new SurfaceHolder.Callback() {

                    @Override
                    public void surfaceCreated(SurfaceHolder surfaceHolder) {
                        try {
                            if (ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA},CAMERA_REQUEST_ID);
                                return;
                            }
                            mCameraSource.start(mCameraView.getHolder());
                       
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)  {
                   
                    }

                    @Override
                    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                        mCameraSource.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();
                        mTextView.post(new Runnable() {
                            @Override
                            public void run() {
                                if (items.size() == 0) {
                                    mTextView.setText("");
                                    mTextView.setVisibility(View.INVISIBLE);
                                } else {
                                    StringBuilder stringBuilder = new StringBuilder();
                                    TextBlock text = null;
                                    for (int i = 0; i < items.size(); i++) {
                                        if ((i < items.size()) && (i < 10)) {
                                            String str = ((TextBlock)items.valueAt(i)).getValue().replaceAll("[^0-9]", "");
                                            Log.d("tag", "Value: " + str);
                                            if ((str.length() == 16) || (str.length() == 19)) {         //read card No.
                                                BA.Log("Value: " + str);        
                                                Intent mIntent = new Intent();
                                                mIntent.putExtra(mTextBlock, str);
                                                MainActivity.this.setResult(0, mIntent);
                                                items.clear();
                                                finish();                                                           // close activaty
                                            }
                                        }
                                        else
                                        {
                                        return;
                                        }
                                    }
                                    mTextView.setText(stringBuilder.toString());
                                    mTextView.setVisibility(View.VISIBLE);
                                }
                            }
                        });
                    }
                });
            } else {
                Log.d(getClass().getSimpleName(), "Text Recognizer isn't ready yet");
            }
        }

    }

    use _scanned_textet event get text result:
    Code:
    public class traWrapper extends AbsObjectWrapper<MainActivity> {

     
        @Hide
        
    public static BA ba;
        @Hide
        
    public static String eventName;
        
    private static MainActivity cv;
        
    private IOnActivityResult ion;
        
    private static final int RC_OCR_CAPTURE = 9003;
     
        
    Intent mIntent;
     

     
        
    public void Initialize(BA paramBA, String paramString) {
            eventName = paramString.toLowerCase(BA.cul);
            ba = paramBA;

            MainActivity cv = new MainActivity();
            
    String str = paramString.toLowerCase(BA.cul);
     
            setObject(cv);
            mIntent = new 
    Intent(BA.applicationContext, MainActivity.class);
     
        
    }


         public void startDetection() {
            ion = new IOnActivityResult() {
           
                @Override
                public void ResultArrived(int arg0, Intent data) {
                    BA.Log("ResultArrived");
                    if (data != null) {
                        String text = data.getStringExtra(MainActivity.mTextBlock);
                        BA.Log("Text read: " + text);
                          if (traWrapper.ba.subExists(traWrapper.eventName + "_scanned_text")) {
                            traWrapper.ba.raiseEventFromDifferentThread(this, null, 0, traWrapper.eventName + "_scanned_text", true, new Object[] {text});
                          }              
                   
                    } else {
                        //statusMessage.setText(R.string.ocr_failure);
                        BA.Log("No Text captured, intent data is null");
                    }
                }
            };
            ba.startActivityForResult(ion, mIntent);
        }
     

    }

    111.png
     
    Last edited: Aug 5, 2018
    Johan Schoeman likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice