Android Open Source - craftar-example-android-image-recognition Craftar Camera Fragment






From Project

Back to project page craftar-example-android-image-recognition.

License

The source code is released under:

MIT License

If you think the Android project craftar-example-android-image-recognition listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.craftar.fragmentexample;
/*w  ww.ja  v  a2  s  . c  o  m*/
import java.util.ArrayList;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.craftar.CraftARActivity;
import com.craftar.CraftARCameraView;
import com.craftar.CraftARCloudRecognition;
import com.craftar.CraftARCloudRecognitionError;
import com.craftar.CraftARItem;
import com.craftar.CraftARResponseHandler;
import com.craftar.CraftARSDK;
import com.craftar.craftarexamplesir.R;

public class CraftarCameraFragment extends Fragment implements CraftARResponseHandler{

  private static final String TAG = "CraftARFragmentSlider";
  private CraftARCloudRecognition mCloudRecognition;
  private String mCollectionToken = "craftarexamples1";
  private boolean mIsCraftarInitialized = false;
  private CraftARActivity mCraftARActivity;
  private View mScanningView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
        // Inflate the layout containing a title and body text.
        ViewGroup rootView = (ViewGroup) inflater
                .inflate(R.layout.fragment_camera, container, false);
        CraftARCameraView cameraView = (CraftARCameraView) rootView.findViewById(R.id.camera_preview);
        mScanningView = rootView.findViewById(R.id.layout_scanning);
        mCraftARActivity =  (CraftARActivity)getActivity();
    mCraftARActivity.setCameraView(cameraView);
    
        initCraftAR();
        return rootView;
    }
    
    
    private void initCraftAR(){
      
      //Initialize the SDK. From this SDK, you will be able to retrieve the necessary modules to use the SDK (camera, tracking, and cloud-recgnition)
    CraftARSDK.init(mCraftARActivity.getApplicationContext(),mCraftARActivity);
    mCloudRecognition = CraftARSDK.getCloudRecognition();
    mCloudRecognition.setCollectionToken(mCollectionToken);
    mCloudRecognition.setResponseHandler(this);
    mCloudRecognition.startFinding();
      mScanningView.setVisibility(View.VISIBLE);
      
    mIsCraftarInitialized = true;
    }
    
    public void onCameraHiddenChanged(boolean hidden){
      if(!mIsCraftarInitialized){
      Log.d(TAG,"CraftAR not initialized...");
          return;  
      }
      
      if(hidden){
      Log.d(TAG,"STOP FINDER MODE!!!");
          mCloudRecognition.stopFinding();
          mScanningView.setVisibility(View.GONE);
      }else{
        Log.d(TAG,"START FINDER MODE!!!");
        mCloudRecognition.startFinding();
          mScanningView.setVisibility(View.VISIBLE);
      }
    }
  @Override
  public void connectCompleted() {

  }

  @Override
  public void requestFailedResponse(int requestCode,CraftARCloudRecognitionError responseError) {
    Log.d(TAG,"Received error with code " +responseError.getErrorCode() +" and message "+responseError.getErrorMessage());
  }

  @Override
  public void searchCompleted(ArrayList<CraftARItem> items) {
    if(items.isEmpty()){
      Log.d(TAG,"Nothing found!");
      return;
    }
    
    mCloudRecognition.stopFinding();
      mScanningView.setVisibility(View.GONE);
    for(CraftARItem item:items){
      String text= "Found item with name :"+item.getItemName();
      Log.d(TAG,text);
      Toast.makeText(getActivity().getApplicationContext(),text,Toast.LENGTH_SHORT).show();
    }
  }
}




Java Source Code List

com.craftar.craftarexamplesir.HowToActivity.java
com.craftar.craftarexamplesir.LaunchersActivity.java
com.craftar.craftarexamplesir.RecognitionFinderActivity.java
com.craftar.craftarexamplesir.RecognitionOnlyActivity.java
com.craftar.craftarexamplesir.SplashScreenActivity.java
com.craftar.craftarexamplesir.WebActivity.java
com.craftar.fragmentexample.CraftarCameraFragment.java
com.craftar.fragmentexample.EmptyFragment.java
com.craftar.fragmentexample.ScreenSlideActivity.java