DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

[DZone Research] Observability + Performance: We want to hear your experience and insights. Join us for our annual survey (enter to win $$).

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Ultimate Guide to FaceIO
  • Why Pub/Sub Isn’t Enough for Modern Apps
  • Scaling Salesforce Apps Using Heroku Microservices - Part 2
  • GraphQL Made Easy With Ballerina

Trending

  • DZone's Article Submission Guidelines
  • A Guide to Data-Driven Design and Architecture
  • Demystifying Project Loom: A Guide to Lightweight Threads in Java
  • REST vs. Message Brokers: Choosing the Right Communication
  1. DZone
  2. Coding
  3. Languages
  4. Capturing camera/picture data without PhoneGap

Capturing camera/picture data without PhoneGap

Raymond Camden user avatar by
Raymond Camden
·
May. 21, 13 · Interview
Like (0)
Save
Tweet
Share
15.96K Views

Join the DZone community and get the full member experience.

Join For Free

As people know, I'm a huge fan of PhoneGap and what it allows me to do with JavaScript, HTML, and CSS. But I think it is crucial to remember that you don't always need PhoneGap. A great example of that is camera access. Did you know that recent mobile browsers support accessing the camera directly from HTML and JavaScript? Let's look at an example.

Over a year ago I wrote a blog post where I created an application called "Color Thief." This application made use of PhoneGap's Camera API and a third party JavaScript library called Color Thief. I loved this example because it demonstrated how you could combine the extra power that PhoneGap provides along with existing JavaScript libraries.

This morning I watched an excellent Google IO presentation (https://www.youtube.com/watch?v=EPYnGFEcis4&feature=youtube_gdata_player) on Mobile HTML. It was an overview of some of the exciting stuff you can now do with mobile HTML and JavaScript. To be clear, this was all without using wrappers like PhoneGap.

In one of the examples the presenters discussed the new "capture" support for the input/file field type. This is rather simple to implement:

<input type="file" capture="camera" accept="image/*" id="takePictureField">

If supported (recent Android and latest iOS), the user can then use their camera to select a picture. I decided to rebuild my old demo to skip PhoneGap completely and just make use of this feature. Here's the code:

<!DOCTYPE HTML>
<html>
	<head>
	<meta name="viewport" content="width=320; user-scalable=no" />
	<meta http-equiv="Content-type" content="text/html; charset=utf-8">
	<title>ColorThief Demo</title>
	
	<script type="text/javascript" charset="utf-8" src="jquery-2.0.0.min.js"></script>
	<script type="text/javascript" charset="utf-8" src="quantize.js"></script>
	<script type="text/javascript" charset="utf-8" src="color-thief.js"></script>
        
	<style>
	#yourimage {
		width:100%;	
	}
	
	#swatches {
		width: 100%;
		height: 50px;	
	}

	.swatch {
		width:18%;
		height: 50px;
		border-style:solid;
		border-width:thin;	
		float: left;
		margin-right: 3px;	
	}
	</style>
	</head>

	<body>

		<input type="file" capture="camera" accept="image/*" id="takePictureField">
        
		<img id="yourimage">

		<div id="swatches">
			<div id="swatch0" class="swatch"></div>
			<div id="swatch1" class="swatch"></div>
			<div id="swatch2" class="swatch"></div>
			<div id="swatch3" class="swatch"></div>
			<div id="swatch4" class="swatch"></div>
		</div>
		
    <script>
	var desiredWidth;

    $(document).ready(function() {
        console.log('onReady');
		$("#takePictureField").on("change",gotPic);
		$("#yourimage").load(getSwatches);
		desiredWidth = window.innerWidth;
        
        if(!("url" in window) && ("webkitURL" in window)) {
            window.URL = window.webkitURL;   
        }
		
	});

	function getSwatches(){
		var colorArr = createPalette($("#yourimage"), 5);
		for (var i = 0; i < Math.min(5, colorArr.length); i++) {
			$("#swatch"+i).css("background-color","rgb("+colorArr[i][0]+","+colorArr[i][1]+","+colorArr[i][2]+")");
			console.log($("#swatch"+i).css("background-color"));
		}
	}	
	
    //Credit: https://www.youtube.com/watch?v=EPYnGFEcis4&feature=youtube_gdata_player
	function gotPic(event) {
        if(event.target.files.length == 1 && 
           event.target.files[0].type.indexOf("image/") == 0) {
            $("#yourimage").attr("src",URL.createObjectURL(event.target.files[0]));
        }
	}
	
	
        
    </script>    
	</body>

</html>

For the most part, this is pretty similar to the last version. I no longer wait for the deviceready event, but instead just listen for the document itself to load. Instead of listening for a button click, I've switched to a input field using type=file. I now listen for the change event, and on that, I see if I have access to a file. If I do, I can then use the URL object to create a pointer to the source and then simply add it to my DOM. After that, Color Thief takes over.

The only tricky part I ran into was that in iOS the URL object is still prefixed. You can see how I get around that in the startup code. To be fair, this isn't 100% backwards compatible, I could add a few checks in here to ensure that things will work and gracefully let people on older phones know they can't use this feature.

But the end result is nearly the exact same functionality in a web page - no PhoneGap, no native code.




JavaScript library Data (computing) mobile app HTML Event Object (computer science) CSS Pointer (computer programming)

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Ultimate Guide to FaceIO
  • Why Pub/Sub Isn’t Enough for Modern Apps
  • Scaling Salesforce Apps Using Heroku Microservices - Part 2
  • GraphQL Made Easy With Ballerina

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: