QML image loading issue - image

I can load an image stored in qrc resource at start up just fine but when I try to load the very same image (for demo purposes only), I get a 'QML Image: Cannot open: qrc:../....' error. Clicking on the image repeatedly will even give the same error to the same PNG file that was loaded at startup (with no error). It's as if the paths change once the program is up and running.Essentially, neither 'gray_button1.png' nor 'gray_button2.png' can be opened for reasons unknown once the program is running...
One other fact:the QML file running this script itself is stored/located in the QML.qrc file.
import QtQuick 2.7
Rectangle {
id: baseBtn
color: "transparent"
property string activeSource:"qrc:../Root/Images/gray_button1.png";
property string inactiveSource:"qrc:../Root/Images/gray_button2.png";
property string previousText:""
property bool active:false
onActiveChanged:{
if (active)
btnImage.source = activeSource;
else
btnImage.source = inactiveSource;
}
MouseArea{
id: mouseArea1
anchors.fill:parent
onClicked: {
active = !active;
}
Image {
id: btnImage
width:parent.width
height: parent.height
anchors.horizontalCenter: parent.horizontalCenter
source:"qrc:../Root/Images/gray_button2.png"; //opens and loads fine at start up -> appears normal
z:0
}
}
}
Here is the file structure
Application
-->Root
---->Images
gray_button1.png
gray_button2.png
-->QML
qml.qrc
button.qml
...{*.qml}

Without being able to see your full project (including the qrc XML, for instance), it's a bit difficult to be able to suggest something concrete – I can't recall a problem like this one — but I'll try.
Unfortunately, Image is a little shy when it comes to reporting what exactly went wrong. I have a fix pending for this, but it probably won't be available until Qt 5.9 (though if you have your own Qt build, feel free to try it out, it might help you!)
If you aren't able to do that, I'd suggest trying to read the paths you're passing to Image using QImageReader (in C++), and making sure that what you are reading makes sense. You should be able to more easily access an error message there through printing the errorString() of the file, something like so:
QImageReader reader("qrc:/Root/Images/gray_button2.png");
QImage img = reader.read();
if (img.isNull()) {
qWarning() << "Something is wrong:" << reader.errorString();
}
If you find no problem that way, then I'd suggest simplifying the existing code you have, for instance, using absolute rather than relative paths, like qrc:/Root/Images/Whatever.png, taking the qrc out of the equation altogether, and continuing that way, cutting one piece after another out of this until you reach something that makes sense (and works, ideally reveals the culprit).
For what it's worth, I can't reproduce the problem you mention with the example QML you're showing here.

Related

Processing: Code stops working without making any changes to it

So I've run into this issue a few times using Processing (p5.js mode), where seemingly out of the blue it doesn't want to compile.
The error reads: "SyntaxError: Expected ; but found [name of my first variable]"
I can literally copy and paste the code into a new processing sketch and it runs. It's just annoying that I have to do that.
The last thing I did this time before it stopped working was rename the sketch. I can't remember if that's what happened with the other times I've had this issue, but I'm betting I would have been aware of that if so.
Any guidance on how to avoid this problem & correct it when it happens would be greatly appreciated.
Thanks
edit I should have mentioned that if I just double click the HTML file, the sketch runs fine. It's only when I try to run it in processing that I get the error.
edit number two on a different laptop with a new download of processing, the same error occurs in the IDE, while the sketch runs if I just double click the HTML file. I'm left to believe the issue starts and stops with the processing IDE.
This is a known bug (or, arguably, a feature) in the p5js mode of the Processing IDE. This mode apparently has an internal JavaScript parser which is stuck in ES5. For example, the following code works fine in any major browser:
let x = 0;
let y = 0;
function setup() {
createCanvas(200,200);
background(0);
fill(255);
x = 0;
y = width/2;
}
function draw() {
rect(x,y,10,10);
x = (x+1)%width;
y = (y+random(5))%height;
}
but you will run into problems when you try to work with it in the p5.js mode of the PDE. The problem disappears when let is replaced by var. Similar issues arise if you try to use code like myArray.map(x => x*x)
The p5.js mode seems like a small project with very little new code within the last year, so I wouldn't hold my breath on a fix. The github description of the project in one place says:
As with the rest of Processing, if you outgrow this setup, you should
use another IDE or development solution (like a full-featured
programmer's text editor and similar tools). We have no interest in
creating a JavaScript IDE. Also like the rest of Processing, we want
people to outgrow this setup.
Sounds like you have outgrown the set up. I recommend using something like Brackets instead.

Check if an image contains another image

Im trying to check if a screenshot contains an image that is saved in the project resources, I need to find a 100% match only and would like to not use any extra libraries, now with that being said, I have no idea how to do so.
heres a few questions:,
do I compare the two buffered images together? do I change them into something else?
Il have to compare it atleast once a second or so. (just as general information)
I have a resource folder under my project in eclipse and the .png files are shown as text, is there a way to change that? I tried tinkering with the settings, no luck yet.
public static BufferedImage screenshot(){
BufferedImage capture = robot.createScreenCapture(screenSize);
return capture;
}
this is my screenshot, another bufferedimage is for example "compare", where the size of the compared image is smaller than the screenshot. how will I be able to check if the image contain the second image?
*For those who wondering, im trying to make a simple program that clicks a certain image once it pops up.

Alternatives to using a MovieClip or BitmapData for an image?

I've been trying for two days to find an alternative to loading an image into my current project. I am using Adobe Flash Professional CS6 as my IDE and Animation program. I would like to be able to display an image in my application. What I am trying to do is have the image display onto the screen, the user enters the PLU associated with the image, and if the PLU is right then they receive a point. I have everything else already to go, but I just can't find an efficient way to deal with loading the image.
Right now I'm using this to accomplish getting my image on the display:
var myDisp:Layer0 = new Layer0();
var bmp:Bitmap = new Bitmap(myDisp);
spDispBox.addChild(bmp);
The above code works just find, but the limitation I can't get around is that I'm going to have to import each image into the library and then consecutively code each part in. I wanted to stick to OOP and streamline this process, I just don't know where I should turn to in order to accomplish my project goal. I'm more than happy to give more information. Thanks in advance, everyone.
July, 26, 2014 - Update: I agree, now, that XML is the way to go. I'm just having a hard time getting the grasp of loading an external XML file. I'm following along, but still not quite getting the idea. I understand about creating a new XML data object, Loader, and URLRequest. It's just loading the picture. I've been able to get output by using trace in the function to see that the XML is loaded, but when I go to add the XML data object to the stage I'm getting a null object reference.
I'm going to try a few more things, I just wanted to update the situation. Thanks again everyone.
it seems like these images are in your FLA library. To simplify your code you can make a singleton class which you can name ImageFactory (factory design pattern) and call that when needing an image which will return a Sprite (lighter than a MovieClip)
spDispBox.addChild( ImageFactory.getImageA() ); // returns a Sprite with your image
and in your ImageFactory
public function getImageA():DisplayObject {
var image:Layer0 = new Layer0(); // image from the FLA library
var holder:Sprite = new Sprite();
holder.addChild( new Bitmap( image ) );
return holder;
}
also recommend using a more descriptive name than Layer0

Embed images using FlashDevelop - AS3

I'm not finding much documentation on embeding images in .as files. Really, I'd like to have some theory on it. So far from what I got reading here and there:
I placed an image in Assets folder inside src. Then right-clicked the image and clicked "Generate embed code", then this code line appears where the cursor was [Embed(source="fluffybunny.png")] what now? How do I assign it to a variable or something... I really didn't find it out there.
Instead of given object using .graphic atribbutes I want to use an image.
Also, does it have to be an .SWF?
There are quite a few resources on this (when you search for "as3 use embed tag"). Some of them are really helpful:
http://www.bit-101.com/blog/?p=853
http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf680e1-7ffe.html
The most basic thing is that you declare a variable of some type. And you use the [Embed] tag before the variable declaration. It's some kind of weird association. Something like:
[Embed(source="image.jpg")]
public var imageClass:Bitmap;
// later on you can instantiate it and use:
addChild(new imageClass()); // creates and adds new image
This is just a sample - there are a lot of types and ways to do it - give a shot the Adobe reference, there are tons of samples.

How to smooth images loaded from server in ActionScript3

I'm trying to smooth an scaled image loaded from another website. The image is not animated.
It works well if I use a local image. but it seems not work with images loaded from remote server.
Here is the snippet:
...
//_loader.load(new URLRequest(http://img.example.com/remote.jpg));
_loader.load(new URLRequest("../assets/local.jpg"));
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
...
protected function completeHandler(event:Event):void
{
var image:Bitmap = Bitmap(event.target.content);
image.smoothing = true;
image.pixelSnapping = "never";
}
As tested, when I load local.jpg, it works perfect. But when I load remote.jpg from the server, the smoothing param didn't work.
Anyone knows why?
I searched everywhere, but no one has the same problem. I'm not using Flash Professional, it's a pure ActionScript Project built in Flash Builder. And the image is not animating. So wired...
Because you are pulling an image from a remote server you need to set a cross domain policy xml file on the web server where the image is held.
Without this you can't alter bitmaps at a sub pixel level.
Example of:
http://www.senocular.com/pub/adobe/crossdomain/policyfiles.html
More details
http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.edu.html
I searched day by day, and finally find the answer:
_loader.load( new URLRequest("http:…." , new LoaderContext(true));
The most important is the second param of load() method, it's a LoaderContext. Reference:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html#load()
Although I set the crossdomain file in the server, without the "new LoaderContext(true)", it won't read the crossdomain file. That's why it doesn't work at first.
If you have the same problem, hope it's helpful to you!

Resources