Customize div background with an image captured by a camera

I am currently working on developing a mobile application using PhoneGap for Android. One of the tasks I am trying to accomplish is setting the background of a div element to the photo captured using the device's camera.

The camera functionality is working smoothly with the provided code. However, my goal is to automatically set the background image of the body div to the captured photo immediately after it is taken, rather than just displaying it to the user.

var pictureSource;
var destinationType;

document.addEventListener("deviceready",onDeviceReady,false);

function onDeviceReady() {
    pictureSource=navigator.camera.PictureSourceType;
    destinationType=navigator.camera.DestinationType;
}

function onPhotoDataSuccess(imageData) {
  var smallImage = document.getElementById('smallImage');
  
  smallImage.style.display = 'block';

  smallImage.src = "data:image/jpeg;base64," + imageData;

  $('#wrapper').css("background-image", "url(imageData.jpg)");
}

function onPhotoURISuccess(imageURI) {
  var largeImage = document.getElementById('largeImage');

  largeImage.style.display = 'block';

  largeImage.src = imageURI;
}

function capturePhoto() {
  navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50 });
}

function capturePhotoEdit() {
  navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true }); 
}

function getPhoto(source) {
    navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
    destinationType: destinationType.FILE_URI,
    sourceType: source });
}

function onFail(message) {
  alert('Failed because: ' + message);
}

Previously, I attempted to set the background using this line of code:

$('#wrapper').css("background-image", "url(imageData.jpg)")

Any assistance or suggestions would be greatly appreciated.

Answer №1

If you're looking for the URI instead of base64 data, make sure to follow these steps:

Simply add this line of code:

$('#wrapper').css("background-image", "url("+imageURI+")")

Place it at the end of the function onPhotoURISuccess and use it as your success handler when calling capturePhoto(). I've tested this myself with similar code taken from phonegap's camera API documentation and it worked perfectly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to retrieve JSON data using Vue and Axios?

I'm facing an issue trying to retrieve product data from a JSON file. Despite attempting different methods and searching for solutions online, I have not been able to find one that fits my specific scenario. As I am new to both Vue and axios, I apprec ...

Ways to utilize jQuery for intricate forms in place of RJS

Within the 74th episode of Railscasts, Ryan demonstrates the process of executing intricate forms with RJS. Check it out here: The key here is to generate a partial and incorporate it using RJS. This episode is quite dated and back then, jQuery may not ha ...

Typescript's array of functions

In my code, I currently have an engage(ability: number, opponent: Creature) function that is responsible for executing one of three different attacks based on the ability chosen. strike(opponent: Creature){} claw(opponent: Creature){} fireball(opponent: C ...

The nav bar in the React CSS is being populated with elements

Currently, I am in the process of developing a React app that includes a navigation bar. The issue at hand is that the content within my h1 tags and other elements are overlapping with the navigation bar - meaning that they are not properly contained withi ...

Exploring the Functions of JavaScript with Google's Cloud Storage

I have been attempting to upload images into buckets on Google Cloud Storage using the JSON API and the Javascript sample provided in the documentation. Although I am able to successfully upload pictures, I am encountering an issue where it prompts me to ...

Access Denied unable to retrieve Android device location

When setting up the Android application, I granted permission for geolocation tracking, mobile lacs, cell ID, country code, and tower code. However, midway through, I encountered an error that said "Permission Denied cannot get location Android," indicatin ...

Guide on filling accordion with data from Firebase

I have been working on a web page that retrieves data from my Firestore collection and is supposed to display each document with its corresponding fields. The goal is to populate the accordion with data from Firebase, but unfortunately, nothing is showing ...

Switch statement not functional for conditionally displaying image in React

Hey there, I'm working on displaying different hangman pictures based on the value of props.lives. import React from 'react'; import one from '../images/one.png'; import two from '../images/two.png'; import three from &a ...

Set maximum size for background image in div

I'm facing some challenges with setting the maximum height of a background image within a div. I want the max height to be equal to the actual height of the image, without stretching it. Ideally, if there is excess content in the div, it should stop a ...

Could it be an issue with Jquery? Maybe it's related to AJAX reload, or possibly just a case

Utilizing the following Jquery : $('#resultstable').on('click', 'tbody tr', function() { alert("helloworld") $('#resultstable tr.Selected').removeClass('Selected'); $(this).addClass('Selected'); ...

Personalized validation using Bootstrap V5

Currently, I am using the default Bootstrap V5 form validator and I am interested in finding a way to create a custom parameter that must be checked for the input to be considered valid. Specifically, I want users to input their license plate, which should ...

The hiding/showing of elements is not being executed correctly by jQuery

My web template includes HTML and jQuery code for a project I'm working on. During the $.getJSON call, there can be a delay in loading the data. To inform the user to wait, I added a warning message in a div with the id="warning". The code properly ...

How can the vertical scroll bar position be reset in Material-Table when pagination is activated?

Whenever I scroll up or down in my Material-Table, and then switch pages, the vertical scroll bar doesn't reset to the top of the table. Instead, it stays where it was before changing the page, causing confusion for users. The only mention of scroll ...

Is it possible to prevent users from clicking on a jQuery UI Datepicker?

Is it possible to disable the functionality of a jQuery UI Datepicker so that clicking on it does nothing but display the date? I attempted: $("#calendar").datepicker({ disabled: true, altField: '#note_date', maxDate: 0 }); Unfortu ...

When clicking on a different tab, the Bootstrap tab will shift to the left, outlining the new selection

While working with Bootstrap, I created a small footer with a tab. However, whenever I click on the second value, the entire div shifts to the left. How can I keep it in place? Below is my code along with two images depicting how the first tab aligns corr ...

What is the reason for Javascript's inability to apply height using the ex unit measurement?

Is there a way to use JavaScript in order to increase the height of an element based on its current CSS height value? I've been able to do it using px units, but not with ex units. Can anyone provide a solution for this? Here is the HTML structure: ...

method for sorting labels in Select element in ReactJS

Hey there, I'm facing an issue with the code snippet available here. I would really appreciate it if you could assist me in resolving this problem. This is the code: import React from "react"; import { Select } from "antd" ...

No results found for the regex pattern

In the code below, I have implemented a regular expression to detect URLs in a string and convert them into hyperlinks: <body> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunct ...

Issues with Callbacks Inquiry

I'm struggling to understand how to implement callbacks in this scenario. Despite researching and reading explanations, I can't seem to grasp the concept. Below is a snippet of my code: function retrieveTransactionInfo(transactionUrl) { re ...

What steps are needed to enable the keyboard on a Otree application for mobile devices?

I previously utilized an Implicit Association Task (IAT) in an experiment conducted on computers. However, I now need to adapt this IAT for use on tablets or mobile devices. Here is how the IAT appears on a cellular device: https://i.stack.imgur.com/kNwo ...