Unity set default image sprite programmatically - android

I am trying to create inputfields in Unity 3D programmatically. I succeeded in this when running in the editor emulator, but trying to built into an android device just provides an error. "BCE0005: Unknown identifier: 'AssetDatabase'." Apparently this AssetDataBase is only available in editor.
inputFieldGO.AddComponent.<Image>();
var image : Image = inputFieldGO.GetComponent.<Image>();
image.sprite = AssetDatabase.GetBuiltinExtraResource.<Sprite>("UI/Skin/InputFieldBackground.psd");
image.type = Image.Type.Sliced;
How do I get around this? How do I set the sprite of this image to the default InputFieldBackground entirely programmatically, without using the AssetDataBase? I'd move the InputFieldBackground into the project resources, but I don't know where the file is or if it's even accessible.

AssetDatabase is an Editor class, that means that can be used in Editor but not in devices.
Unity Scripting Reference For AssetDatabase
Solution:
Do you have your files on Resources folder?
Try this:
Sprite newSprite = Resources.Load<Sprite>(spritePath);
From: Unity Scripting Reference for Resources.Load

Related

OpenCV for Android: How to use YOLOV4 DNN models in an app

I have been creating an app in Android Studio which uses OpenCV. I have my YOLOV4 models which I can use to detect humans. When I was using the Dnn class of org.opencv.dnn.Dnn, I need to provide the absolute path of the config and weights. Initially, I tried to put the files in my assets. But I was not able to get the path of the assets file.
I have tried putting the config and weights file in the external storage of my AVD. But it is showing -
Error: Parsing error (Failed to open NetParameter file: /storage/emulated/0/dnns/yolov4-tiny.cfg) in readNetFromDarknet
This is my code -
String yoloCfg = Environment.getExternalStorageDirectory().getAbsolutePath() + "/dnns/yolov4-tiny.cfg";
String yoloWeights = Environment.getExternalStorageDirectory().getAbsolutePath() + "/dnns/yolov4-tiny.weights";
yoloModel = Dnn.readNetFromDarknet(yoloCfg, yoloWeights);
Am I doing anything wrong or is it an issue?

How to read an image into a Mat object using opencv c++ and android studio ndk?

I have searched for a few hours now and can't seem to find an answer to my question(s). I have written the following lines of code in the android ndk(c++) and I am using the needed opencv libraries to accomplish the task.
void opening_images(){
Mat image ;
sillyString = "I have changed";
String imagePath = "//drawable//ring.png";
image = imread(imagePath,IMREAD_COLOR);
if(image.empty()){
sillyString = "Image not loaded";
}
else {
sillyString = "Image loaded";
}
}
I have tested this code in Qt with opencv and it works fine.At the moment the program in android studio returns the "Image not loaded" string. I think the main problem which is present is, the fact that I don't completely understand how to work with the file paths? In android studio I have included a picture under res/drawable/ring.png. I am able to view this image using the java side of the app.
Question 1: Is the specified imagePath = "//drawable//ring.png" correct to access the ring.png file ?
Question 2: Is there any permissions needed allowing the ndk to access res folders ?
Question 3: Is there any similar methods to assign an image to a Mat object?
Any help will be appreciated.
Edit:
If you take a look at how BitmapFactory decode resource works - you will see that getting bitmap from drawable still requires unpacking of a compressed image.
So answer to your q1: No it is not correct way to access ring.png, you will either have to download resource to your device or unpack it to byteArray and use imdecode instead of imread
Since this was my first time using Android Studio there was much to learn. It took me a while but here is the answers to the questions that I posted.
Question 1: Is the specified imagePath = "//drawable//ring.png" correct to access the ring.png file ?
This is most definitely not the correct path to use when accessing images for the purpose of image processing etc.The drawable folder can still be used to update ex. an image view by setting the src of the image view to the image
imageView.setImageResource(R.drawable.ring);
When working with images and Mat objects, I found it best to use the Android debugging bridge to copy the files to the SD card of the device. This link will provided you with the necessary steps to install the adb https://www.howtogeek.com/125769/how-to-install-and-use-abd-the-android-debug-bridge-utility/
When the images are done copying to the SD card. The file path can be found by making use of the built in Java functionEnvironment.getExternalStorageDirectory(). Which looks something like /storage/emulated/0/YOUR_file it depends on the location which was used to copy the files to.
Useful tip: Download ES File Explorer for the device to help navigate through the external or internal storage.
Question 2: Is there any permissions needed allowing the ndk to access res folders ?
The method which I used didn't need any permissions. However at the moment the NDK side cannot directly read an image from the SD card, the image must be passed from the Java side by making use of assets or by passing the address of the image which was read into the Mat object(Java side).
Read and write permission is needed in order to access the SD card. This must be set in the manifest.xml and must be correctly implemented in the code. There are many great tutorials on YouTube.
Question 3: Is there any similar methods to assign an image to a Mat object?
This question seems redundant now, there are many ways to skin a cat.
In short, I think it is easier to stick to the Java side when using Opencv4Android and some form of image processing is needed.To get you started in java here is a small snippet from my code.
Mat image;
String imageInSD = Environment.getExternalStorageDirectory()+"/Pictures/Data/Birds/"(ImageFolders[i])+"/"+String.valueOf(id)+".png";
image = Imgcodecs.imread(imageInSD,Imgcodecs.IMREAD_COLOR);
Good luck!!
Another way to use saved image in NDK is follows,
Then drawable folder, you can save it in assets folder. This helps to access multiple images also easily.
Then BitmapFactory.decodeStream helps to take it as bitmap and Utils.bitmapToMat is used to convert bitmap image to Mat file.
Then this Mat file, you can pass to NDK and process it using OpenCV C++.
Thanks

Air for android ignores external swf files

I am working on some app that loads external swfs from the application directory
the problem is that some swf files get loaded correctly and others give ioerror url not found
i put the paths in an array and use a loader to load the path
var arr:Array = ["Games/1.swf", "Games/2.swf"];
var loader:Loader = new Loader();
loader.load(new URLRequest(arr[0]));
this is just example and it is the same in loading all the files but does not work on all files.
what would be the problem?
Firstly, whenever you load something, listen for the appropriate error events so you know what's going on:
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
When using mobile, you should use the app:// shorthand as a reference to the application directory (which I assume is where your Game folder is. Relative paths do not usually work.
So it should look like this:
var arr:Array = ["app://Games/1.swf", "app://Games/2.swf"];
For more information/options, you can look at my answer to this question

Titanium 3.X getFile() from local storage

Using Titanium on Android 4+ I want to access a jpeg file which has been taken with the camera. I need to achieve 2 objectives, namely, return the EXIF data and transfer the bytes to an API endpoint. My problem is I'm unable to access the file...
I'm using a 3rd party module to handle the file selection (Multi Image Picker) which returns a list of file locations, using the File Manager app on the emulator (GenyMotion) I can confirm the location on disk is correct. However, the following always returns false...
var file = Ti.Filesystem.getFile('/mnt/sdcard/DCIM/Camera/IMG_20140901_083735.jpg');
Ti.API.info('Do we have a file? ' (file.exists()? 'YES' : 'NO'));
The output for the above would be... Do we have a file? NO
Further reading shows Titanium has 5 predefined folder locations which can be passed into the getFile() method and one possible reason for the above code not working would be it is defaulting to the 'Resouces' folder location? That said all but one folder location is app specific, the exception being externalStorageLocation. Now my understanding of an Android device is that any image taken with the camera will be stored on the internal storage system unless an SD card is present. This is true in my case as the following lists 0 files...
var extDir = Ti.Filesystem.getExternalStorageDirectory();
var dir = Ti.Filesystem.getFile(extDir);
var dir_files = dir.getDirectoryListing();
Ti.API.info('External files... ' + dir_files.length);
The output for the above would be... External files... 0
So am I right in thinking Appcelerator have simply not included the ability to access local storage (outside of any app specific folders) within their API? Or am I missing something and there is in fact another way?
Thanks to #Bharal I was able to find a solution...
By using the Ti.Media.openPhotoGallery() method I was able to identify the correct native path for the image by inspecting the event object returned from the success callback.
The path was missing 'file://' at the beginning, I couldn't be 100% sure but I suspect this forces the getFile() method to use an absolute path and not a relative path from within the Resources folder.
To confirm, the following will return a file object...
var file = Ti.Filesystem.getFile('file://[path]');
Where [path] is the folder location as reported within the File Manager app on the device, for example '/mnt/sdcard/DCIM/Camera/IMG_20140901_083735.jpg'
Yah mon, i dunno.
Here is wat i used when i was doin pictures on my Ti app, but then i got rid of that section because i realised i didn't need to be doin pictures. Pictures mon, dey ain' what you want sometimes, yo?
Ti.Media.openPhotoGallery({ //dissall jus' open up a piccha selectin' ting. ez.
success:function(event){
var image = event.media;
if (event.mediaType==Ti.Media.MEDIA_TYPE_PHOTO){
//so image.nativePath is the path to the image.
// profileImg be jus' some Ti.UI.createImageView ting yo be puttin in yo' page.
//meyybe yo be wantin' alert(image.nativePath); here too, dat be helpin?
profileImg.image = image.nativePath;
}
},
cancel:function(){
//we cancelled out, why we doin' that?
}
});
Now that isn't going to really be helpin' you, but yo can use that to see wat the native path yo piccha be usin' be, and then be seein' if maybe what yo be puttin' in yo code be sam ting.
Jus' wrap the above as an addEventListener("click", function(){ ... } ); on sam ting in yo page, and jus' add sam element to put th' piccha in if yo be wantin' to see the piccha but i be tellin' you picchas mon, sometimes dey ain' worth time.
But meyybe yo wantin' use not an emulator for dis ting, dey can be actin' weird yo should be usin some small phone maybe? Dat way you can be findin' if yo got dem memory leeks and meyybe some memory sprouts, an memory onions too.

How do you integrate custom fonts in an app?

I am trying to integrate custom fonts (Imperator.ttf and goodfish.ttf) from the following website into my corona app:
http://www.1001freefonts.com/top-fonts.php
I am following carefully the tutorial found here on Corona's official website:
http://www.coronalabs.com/blog/2013/01/16/faq-wednesday-custom-fonts/
I follow the whole procedure for macosx integration (installing the font by double clicking etc) and do not get any error. I also added the font file name to my build.settings file:
UIAppFonts =
{
"Imperator.ttf"
},
Whenever i launch the simulator and debug the currently loaded fonts, the new one doesn't appear.
Is there an easy way to debug the issue on corona simulator, on osx, to troubleshoot why the font doesn't load ?
Thanks
While installing custom fonts to your corona application, you have to follow the following steps:
Install the font in your system.
Copy and paste the font file(such as: Imperator.ttf) to the application folder where your main.lua exists.
Then for iPhone(only for iPhone, Android doesn't need this), add the following lines to your build.settings:
iphone =
{
plist =
{
UIAppFonts =
{
"Imperator.ttf" -- Font file name
},
UIApplicationExitsOnSuspend = true
},
}
Debug and get the supported Font Names by the system using the following:
local fonts = native.getFontNames()
for i,fontname in ipairs(fonts) do
print(fonts[i])
end
From the above debugging, you can get the exact Font Name (here: Imperator) that you have to use while creating the text/label. Sometimes this may differ from the name of the font file. You can also get it from applications like photoshop(it's text tool font name), etc.
local myText = display.newText("Label with custom font",150,100,"Imperator",20)
Then finally, you will get the output as:
Keep coding................ 😃
Put Imperator.ttf font file in Assets folder
In class use following code:
Typeface imperator_typeface =Typeface.createFromAsset(getContext().getAssets(),"Imperator.ttf");
your_textview.setTypeface(imperator_typeface);

Categories

Resources