Android Ultimate Plugin Tutorial part 12 – Camera and Share

Hello Every one , i know that you wanted to know on how to use the Camera feature on Android Ultimate Plugin , and today is that day so let’s get started.

First make sure that you have an Android Manifest File  in “Assets/Plugins/Android” and you added this permissions

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />

After added that permissions on your Android Manifest File we can start coding now.

First get the instance of  Custom Camera Plugin

private CustomCameraPlugin customCameraPlugin;
customCameraPlugin = CustomCameraPlugin.GetInstance();

Set Debug to 0 because i don’t want to see any Debug Toast Messages it’s annoying.

customCameraPlugin.SetDebug(0);

Then we initialized the Camera Plugin and pass the folder name where we wanted our image to be save.

private string folderName="AUP_CCP";
private string imageFileName="AUP";
private string imagePath="";
private bool useBackCamera = false;

customCameraPlugin.Init(folderName,imageFileName,useBackCamera);

Note:

folderName will be the name of folder on the actual device

imageFilaName will be the name of Image that you take

ImagePath is actual directory path where you will save the image

useBackCamera is to know if you want to use front or back camera if front just pass false if back pass true

After Initialization we need to setup callback listener for our Camera Plugin and we need to pass our methods onCaptureImageComplete, onCaptureImageComplete and onCaptureImageFail.

customCameraPlugin.SetCameraCallbackListener(onCaptureImageComplete,onCaptureImageCancel,onCaptureImageFail);

 

after we setup our Camera Plugin  we can now start taking picture using our Camera on our Android Device. we can do this by calling this method

customCameraPlugin.OpenCamera();

After taking picture  we will received an events

onCaptureImageComplete – if successful

public void onCaptureImageComplete(string imagePaths){
       dispatcher.InvokeAction(
	 ()=>{
	    // do something here	
	     string[] imagePathCollection = imagePaths.Split(',');
             if(imagePathCollection.Length > 0){
                 this.imagePath = imagePathCollection.GetValue(0).ToString();
                 mediaScannerPlugin.Scan(this.imagePath,"image/jpeg"); 
               }

	  }
	);
}

an Image paths on where our picture has been saved. we can now use this to load the texture by calling Utils.LoadTexture and pass image path on it

 

onCaptureImageFail – if failed

public void onCaptureImageFail(){
       dispatcher.InvokeAction(
	     ()=>{
                 // do something here
	     }
	);
}

onCaptureImageCancel – if camera is cancel

public void onCaptureImageCancel(){
	dispatcher.InvokeAction(
		()=>{
                  // do something here
		}
	);
}

and then we can share it just by passing Image path on Share Image method of SharePlugin together with “Share Subject Title” and “Share message”

public void SharePicture(){
	if(!imagePath.Equals("",StringComparison.Ordinal)){
	        sharePlugin.ShareImage("MyPictureSubject","MyPictureSubjectContent",imagePath);
		UpdateStatus("Sharing Picture");
	}else{
		Debug.Log("[CustomCameraDemo] imagepath is empty");
		UpdateStatus("can't image path is empty");
	}
}

And this is the exact code that we use on our  Custom Camera Plugin  and Share demo

Note:  you need to drag Text and RawImage UI  on this code inspector to make it run and also you need Button UI to access the open camera method.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using AUP;

public class CustomCameraDemo : MonoBehaviour {
	
	private SharePlugin sharePlugin;
	
	public Text statusText;
	public RawImage rawImage;
	
	private CustomCameraPlugin customCameraPlugin;
	private string folderName="AUP_CCP";
	private string imageFileName="AUP";
	private string imagePath="";

	private MediaScannerPlugin mediaScannerPlugin;
	
	public Button shareButton;

	private Dispatcher dispatcher;
	
	// Use this for initialization
	void Start (){
		dispatcher = Dispatcher.GetInstance();

		sharePlugin = SharePlugin.GetInstance();
		sharePlugin.SetDebug(0);
		
		customCameraPlugin = CustomCameraPlugin.GetInstance();
		customCameraPlugin.SetDebug(0);
		customCameraPlugin.Init(folderName,imageFileName,false);
		
		customCameraPlugin.SetCameraCallbackListener(onCaptureImageComplete,onCaptureImageCancel,onCaptureImageFail);

		mediaScannerPlugin = MediaScannerPlugin.GetInstance();
		mediaScannerPlugin.SetDebug(0);
		mediaScannerPlugin.Init();
		mediaScannerPlugin.SetCallbackListener(onScanStarted,onScanComplete,onScanFail);
		
		EnableDisableShareButton(false);
	}
	
	public void OpenCamera(){
		customCameraPlugin.OpenCamera();
		EnableDisableShareButton(false);
		UpdateStatus("Opening Camera");
	}
	
	public void SharePicture(){
		if(!imagePath.Equals("",StringComparison.Ordinal)){
			sharePlugin.ShareImage("MyPictureSubject","MyPictureSubjectContent",imagePath);
			UpdateStatus("Sharing Picture");
		}else{
			Debug.Log("[CustomCameraDemo] imagepath is empty");
			UpdateStatus("can't image path is empty");
		}
	}
	
	private void UpdateStatus(string status){
		if(statusText!=null){
			statusText.text = String.Format("Status: {0}",status);
		}
	}
	
	private void DelayLoadImage(){
		//loads texture
		rawImage.texture = AUP.Utils.LoadTexture(imagePath);
		
		UpdateStatus("load image complete");
		EnableDisableShareButton(true);
	}
	
	private void LoadImageMessage(){
		UpdateStatus("Loading Image...");
	}
	
	private void EnableDisableShareButton(bool val){
		shareButton.interactable = val;
	}
	
	public void onCaptureImageComplete(string imagePaths){
		dispatcher.InvokeAction(
			()=>{
				Debug.Log("[CustomCameraDemo] onCaptureImageComplete imagePaths " + imagePaths);

				string[] imagePathCollection = imagePaths.Split(',');

				foreach( string path in imagePathCollection){
					Debug.Log("[CustomCameraDemo] onCaptureImageComplete path " + path);
				}

				if(imagePathCollection.Length > 0){
					//get the top most image path
					this.imagePath = imagePathCollection.GetValue(0).ToString();
					Debug.Log("[CustomCameraDemo] onCaptureImageComplete imagePath " + imagePath);
					mediaScannerPlugin.Scan(this.imagePath,"image/jpeg");
				}

				UpdateStatus("CaptureImageComplete");
				Invoke("LoadImageMessage",0.3f);
				Invoke("DelayLoadImage",0.5f);
			}
		);
	}
	
	public void onCaptureImageCancel(){
		dispatcher.InvokeAction(
			()=>{
				UpdateStatus("CaptureImageCancel");
				Debug.Log("[CustomCameraDemo] onCaptureImageCancel");
			}
		);
	}
	
	public void onCaptureImageFail(){
		dispatcher.InvokeAction(
			()=>{
				UpdateStatus("CaptureImageFail");
				Debug.Log("[CustomCameraDemo] onCaptureImageFail");
			}
		);
	}

	public void onScanStarted(){
		dispatcher.InvokeAction(
			()=>{
				UpdateStatus("Media Scan Started");
				Debug.Log("[CustomCameraDemo] onScanStarted media ");

			}
		);
	}

	public void onScanComplete(){
		dispatcher.InvokeAction(
			()=>{
				UpdateStatus("Media Scan complete");
				Debug.Log("[CustomCameraDemo] onScanComplete media ");


			}
		);
	}

	public void onScanFail(){
		dispatcher.InvokeAction(
			()=>{
				UpdateStatus("Media Scan fail");
				Debug.Log("[CustomCameraDemo] onScanFail media ");

			}
		);
	}
	
}

I think we are done,  you can now test this code on your Android Device and see what will happen.

I Hope this tutorial some how helps you on dealing with  Custom Camera Plugin

For more Information

http://www.gigadrillgames.com/android-ultimate-plugin/

For more Tutorials

http://www.gigadrillgames.com/2015/07/26/list-of-tutorials-for-android-ultimate-plugin/

For Questions or Comments or any Feed back just email us at gigadrillgames@gmail.com

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.