Display png image from native module in react native - java

I have a slider component which I want to have a handle as png image pass as a prop. Now, I want that png image to pass through react native to native modules(android and ios both). The component is written at native side for both ios(Objective C) and android(Java).
How can I pass png image as a prop to native modules?
How can I use them at native Java part?(I am using Canvas to draw my
slider as shown below).
How can I use them at native Objective C?(I am using CGContext to
draw my slider).
What I have tried:
I resolved path of png image by following code at React Native side.
const myImage = require('../assets/slider-handle.png');
const resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource');
const resolvedImage = resolveAssetSource(myImage);
Then pass the path of image as a prop imagePath={resolvedImage.uri}.
Then at Java part I do following:
public void drawImage(String imagePath, Canvas canvas, int x, int y) {
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
canvas.drawBitmap(bitmap, y, x, somePaintValue);
}
I am getting the path of image(the path is something like this: (http:/10.0.2.2:8081/assets/app/components/slider/assets/slider.png), I have verified that at native side by consoling the path.
Not sure if I need to pass only path of image or maybe there is a way to pass whole png.
This is my first time working with Java and Objective C.
This is the slider I am developing.
Any help will be really appreciated.

Related

Implementing an image upload button using Jython & Swing

I'm developing a cross platform desktop application using Jython and Swing, and I found a hitch.
I would like to develop a button that allows me to load an image in its background and to change it when I reclick on the image loaded.
I attach as an example some pictures on how I would like my widget to be.
Upload pane without image
And then, when I load an image:
Upload Pane with image
I tried with the following code:
fd = FileDialog(self, "Scegli un'immagine", FileDialog.LOAD)
fd.setFile(';*.'.join(("*.bmp","jpg","jpeg","wbmp","png","gif")))
fd.setVisible(True)
fileName = fd.getFile()
if fileName != None :
fileAbsPath = os.path.abspath(fileName)
"""'self.bigDict['imgButton']' is the current JButton"""
self.bigDict['imgButton'].setIcon(ImageIcon(fileAbsPath))
When I click on "Open", the image is not inserted into the button. I do not understand why.
I also tried with the following code:
if fileName != None :
fileAbsPath = os.path.abspath(fileName)
img = ImageIO.read(getClass().getResource(fileAbsPath))
self.bigDict['imgButton'].setIcon(img)
In the above example the following error is reported:
img = ImageIO.read(getClass().getResource(fileAbsPath))
TypeError: getClass(): expected 1 args; got 0
I would be curious to know why the button is not updated with the image loaded, and why the above error in java would not happen.
Thanks in advance!
getClass() needs 1 argument: the implicit this argument. You must call the method on an object, or use the MyClass.class notation.
The problem is very simple.
When an image is loaded with FileDialog it is "virtually located" in the FileDialog window, but the image does not exist. I realized this when I tried to copy the image from the absolute path to the destination folder, using shutil.copy2(self.imgAbsPath, destinationPath+'/'+self.imgName), which reported an error message indicating that the image does not exist.
To provide the concrete path of a file, it is necessary to add information about the folder where it is located.
In practice you have to create the relative path, before generating the absolute one:
fd = FileDialog(self, "Scegli un'immagine", FileDialog.LOAD)
fd.setFile(';*.'.join(("*.bmp","jpg","jpeg","wbmp","png","gif")))
fd.setVisible(True)
self.imgName = fd.getFile()
relativePath = fd.getDirectory() + fd.getFile() """Here is the missing piece!"""
if self.imgName != None:
self.imgAbsPath = os.path.abspath(relativePath) """Here the absolute path is correctly obtained!"""
self.bigDict['imgButton'].setIcon(ImageIcon(ImageIcon(self.imgAbsPath).getImage().getScaledInstance(130, 130, Image.SCALE_DEFAULT)))
"""The image, before being inserted into the button, is slightly resized, to avoid graphic bumps!"""
I hope I've been helpful.

Bitmap and proper saving location

Hi everybody I've got the following problem with an adroid app: I want to read an image saved and perform some operations on it.
(I do not need to use this image to draw something on screen, I just have to check the color of some pixels)
I'm using the following code:
Bitmap bitmap = BitmapFactory.decodeFile("drawable-v24/ean13.bmp");
The fact is that the BitmapFactory returns null as a FileNotFoundException is thrown. (by now the file is saved inside res folder)
I don't really understand where I should put the image and how to give the path to the BitmapFactory to be able to get it properly.
(I guess the problem is due to the fact that the image actually is stored on my pc and not on the Android device but I can't understand how should I proceed)
Thank you in advance for the help! :)
If you are accessing image from resource drawable of mipmap. You should use BitmapFactory.decodeResource to get Bitmap.
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
Or another overloaded version .
public static Bitmap decodeResource(Resources res, int id, Options opts)
where getResources() is method of Activity.

A button which allows an image to be shown in Android Studio

I am building my application using Android Studio, this app can upload an image from raspberry to my emulator. It works fine. What I want to do now is uploading this image and showing it directly to the user without searching it in the gallery. I thought about creating another class and setting this image as a background image in my xml file, but this is too much like I have to create another class every time I want to upload an image from my raspberry.
Can someone help me please. Thank you
If I'm understanding your question correctly, you'd like to load an image from the Android filesystem into your app and display it to the user.
Drawable, Android's generalized image class, allows you to load from file via Drawable#createFromPath.
This SO question suggests Drawable#createFromPath doesn't work on paths beginning with file://, so depending on your use case you may want to precede that with Uri#parse/Uri#getPath.
Once you have a Drawable, you can display it in one of two ways: put an ImageView in your app and call its setImageDrawable method, or set the Drawable as your background image via View#setBackground (note that setBackground was only added in API 16 - in prior versions, you should call View#setBackgroundDrawable).
Putting all of this together, we end up with the following (untested):
private void loadImage(String imagePath) {
Uri imageUri;
String fullImagePath;
Drawable image;
ImageView imageDisplay;
imageUri = Uri.parse(imagePath);
fullImagePath = imageUri.getPath();
image = Drawable.createFromPath(fullImagePath);
imageDisplay = (ImageView) findViewById(R.id.imageDisplay);
/*if image is null after Drawable.createFromPath, this will simply
clear the ImageView's background */
imageDisplay.setImageDrawable(image);
/*if you want the image in the background instead of the foreground,
comment the line above and uncomment this bit instead */
//imageDisplay.setBackground(image);
}
You should be able to modify this to work with any View just by replacing imageDisplay's declared type with the appropriate View type and changing the cast on findViewById. Just make sure you're calling setBackground, not setImageDrawable, for a non-ImageView View.

Good practice loading assets LibGDX HTML (GWT)

I'm creating a game that will mainly run on browser thanks to GWT HTML5 deployment. I have quite a few MB of assets between images and sounds. Obviously I don't need them all at all times. I'd like to load them depending on the screen that's coming next and other parameters. That is specially critical because my main deployment will be on web (can't load all 50MB when starting just in case they will be used).
I don't really know how asset loading works on LibGDX on HTML5 really. I'd like to use the AssetManager or something similar to load my TextureAtlases and Sounds but I think HTML5 deployment just loads everything in the beginning regardless if I later use the AssetManager in the core project.
What would be a correct asset loading pipeline with libGDX and GWT. It would be awesome to load all from the core project because that way would be multiplatform for the future but it is not critical now so the important part is to load assets wisely on the web.
Thanks in advance.
My recommended way of managing assets:
// load texture atlas
final String spriteSheet = "images/spritesheet.pack";
assetManager.load(spriteSheet, TextureAtlas.class);
// blocks until all assets are loaded
assetManager.finishedLoading();
// all assets are loaded, we can now create our TextureAtlas object
TextureAtlas atlas = assetManager.get(spriteSheet);
// (optional) enable texture filtering for pixel smoothing
for (Texture t: atlas.getTextures())
t.setFilter(TextureFilter.Linear, TextureFilter.Linear);
Now to load a sprite you do:
// Create AtlasRegion instance according the given <atlasRegionName>
final String atlasRegionName = "regionName";
AtlasRegion atlasRegion = atlas.findRegion(atlasRegionName);
// adjust your sprite position and dimensions here
final float xPos = 0;
final float yPos = 0;
final float w = asset.getWidth();
final float h = asset.getHeight();
// create sprite from given <atlasRegion>, with given dimensions <w> and <h>
// on the position of the given coordinates <xPos> and <yPos>
Sprite spr = new Sprite(atlasRegion, w, h, xPos, yPos);

displaying a custom map and placing pin/images on the map

i wanted to create a directory app for a grocery. i already have the floor plan of the place. i just wanted to display it in a view and i want to put some markers/pins/images to it.. i have no idea where to start. someone told me to use bitmap and canvass but i have no idea how to use it as the other tutorials in google is quite vague and hard to understand..
what my app requires:
display a custom map from an image source
enable me to put markers,pins and some images on top of it
A simple way is to use use ImageView:
//make a bitmap from your floorPlanImage, example from file
Bitmap floorPlanImage = BitmapFactory.decodeFile(filePath, options);
//make a mutable bitmap, so it can be changed
Bitmap mutableFloorPlanImage = floorPlanImage.copy(Bitmap.Config.ARGB_8888, true);
//create a canvas to draw on your floorPlanImage
Canvas mCanvas = new Canvas(mutableFloorPlanImage);
//draw a marker or another image on the floorPlanImage, you can position it using left and top //coordinates, in this example both 0
mCanvas.drawBitmap(markerBitmap, 0, 0, null);
//show the resulting bitmap in ImageView
imageView.setImageBitmap(mutableFloorPlanImage);

Categories