gl-react-native-v2 icon indicating copy to clipboard operation
gl-react-native-v2 copied to clipboard

[Android] Loading local images – add support for require()

Open AlexDM0 opened this issue 10 years ago • 21 comments

Hi,

In the app I want to make I want the user to take a picture and save it locally, after this it should be processed by gl-react-native when I want to display it.

I tried to do it like this (straight from the image example):

<Saturation factor={0.5} image={{ uri: "../images/room.jpg" }} />

but this does not work, I get a black space where the image should be. If I use an URL like (from the example:

http://i.imgur.com/iPKTONG.jpg

it does work.

I also tried the way React Native does it:

<Saturation factor={0.5} image={require("../images/room.jpg" )} />

But this crashed the simulator (Android).

I can't find where in the code the images are actually being loaded so I'm guessing this is in the native parts. The documentation is only using online URLs so no help there.

Any ideas?

Regards

AlexDM0 avatar Feb 25 '16 18:02 AlexDM0

that require(localFile) that you attempted should be the way to do this, the {uri} form should not be supported for local files. I recently had to fix local file support for iOS (https://github.com/ProjectSeptemberInc/gl-react-native/commit/6fc8611fcb2ce45251fdc329a51d5a793c7f82e4) and I guess something need to be fixed too for the Android version (since RN recently changed the way this works).

gre avatar Feb 25 '16 18:02 gre

Thanks for the quick reply! I look forward to a fix!

Cheers

AlexDM0 avatar Feb 25 '16 18:02 AlexDM0

I asked some weeks ago (here: https://github.com/facebook/react-native/pull/4902#issuecomment-167836411) if there where an equivalent of RCTImageSource class in RN Android impl that would help implementing this, it doesn't seem there is, we will have to dig into the Image Source implementation for the Android case and probably duplicate some code from RN.. (I remember seeing some private function there.. maybe we should PR in RN to expose more stuff for us).

Feel free to investigate on this, I might not have time for this during this week.

gre avatar Feb 25 '16 18:02 gre

Hi,

I'm not familiar at all with android unfortunately. I'll do a pull for the docs tomorrow, any preference for branches?

I'll continue with iOS for now since I need both apps and my Mac is just about finished syncing :).

I'm more familiar with swift and JS so if something comes up there I might be able to help out. For now I have to rough out my app asap :).

Stupid and unrelated question but can I do processing on images without going through the react interface? I figured I'd scale, make b&w etc once after the image has been obtained and save the different version to disk to save processing time later on. If it's not as simple as "yes look here and rtfm" or "no" I can make another issue for it.

Cheers

AlexDM0 avatar Feb 25 '16 18:02 AlexDM0

I'll do a pull for the docs tomorrow, any preference for branches?

You can submit a Pull Request on Github via any of your branch (target master).

Stupid and unrelated question but can I do processing on images without going through the react interface? I figured I'd scale, make b&w etc once after the image has been obtained and save the different version to disk to save processing time later on.

I guess not easily, actually we do have the same exact use-case on our side. The way we have solved this is to write something like this: https://gist.github.com/gre/3a08cf9e82a51b0b160e That might feel hacky and i'm not sure if React/RN provides way to "render things offscreen" (but @sebmarkbage recently told me it was possible, haven't tried yet).

gre avatar Feb 25 '16 18:02 gre

Thanks for the example! I have the PR submitted, hope its useful!

AlexDM0 avatar Mar 01 '16 11:03 AlexDM0

to be done:

  • [ ] make sure textureUniform={require("./image.png")} works
  • [ ] make sure textureUniform={"file:///...a.path.to.an/image.png")} works

ideally try to see if there is a common utility for receive a imagesource in props (like on iOS), otherwise fork the ImageSource java code. The best would also be to PR on react-native to expose a common thing

gre avatar Mar 09 '16 08:03 gre

Hi,

I tried just require in the saturation example on iOS now and the app crashed.

From the advanced example: https://github.com/ProjectSeptemberInc/gl-react-native/blob/master/example/src/AdvancedEffects/index.js#L20

I now use the resolveAssetSource method before and it works. Maybe update it in the docs here: https://projectseptemberinc.gitbooks.io/gl-react/content/docs/examples/2.html

GL.Node supports uniforms props that allow to send to the shader any parameters from the JavaScript world.
In this example, we send the factor number (that come from props) and we also send the image. The image have the same format as the source passed in an <Image/>.

to reflect this?

Regards

AlexDM0 avatar Mar 17 '16 13:03 AlexDM0

This works if you add the resolveAssetSource? I wish React Native would hide this part for us. I'm not happy having to rely on "react-native/some/internal/stuff". I'm not sure how we should address this, maybe gl-react-native should expose and proxy this function in the meantime?

also, Have you checked on Android too ?

gre avatar Mar 17 '16 13:03 gre

In detail:

constructor() {
  super();
  this.image = resolveAssetSource(require('../images/mediaRoom.png'));
}

...

render() {
  return <Surface width={300} height={300} borderRadius={150}>
    <Saturation factor={0.5} image={this.image} />
  </Surface>
}

works, whereas this:

render() {
  return <Surface width={300} height={300} borderRadius={150}>
    <Saturation factor={0.5} image={require('../images/mediaRoom.png')} />
  </Surface>
}

Crashed my simulator app on iOS.

Apart from the rounded borders this also works on Android.

Regards

AlexDM0 avatar Mar 17 '16 13:03 AlexDM0

IN IOS: If I use captureFrame and use RNFS to save it to file, I can find the file in finder, as a functional image too. Then when I put do this in a normal RN Image object:

The path in this case is the following and if I go there there is a correct image:

"file:///Users/alex/Library/Developer/CoreSimulator/Devices/6955A1DF-7189-4FD1-91EE-AFE106019086/data/Containers/Data/Application/F19A2400-3339-4C01-9FAA-FB36AE54C00E/Library/Caches/image.png"

But I get the Requiring unknown module error. Should I raise this at RNFS or did I misunderstand the documentation here: https://projectseptemberinc.gitbooks.io/gl-react/content/docs/api/Surface.html

In gl-react-native, format: "file" allows to save to a file. You also need to provide a filePath String (you should use react-native-fs to knows directory paths). The promise result will be a file://... url that you can use in .

We have also discussed this in the border radius issue but this is a bit different since I thought it should work on iOS.

Regards

AlexDM0 avatar Mar 17 '16 17:03 AlexDM0

Hi,

Nevermind the last question, it was answered over at react native fs.

For others: to use an capturedFrame from GLRN in an RN Image, you can do it like this:

// preload because I use an external image in the surface. When using an external image, if we dont set preload to true the image you save will be empty.
<Surface ref={this.onRef.bind(this)} width={300} height={300} preload={true}>
  ...
</Surface>

...

onRef(input) {
  var path = RNFS.DocumentDirectoryPath + '/image.png';
  input.captureFrame({type:'png', format:'file', filePath: path, quality:1}).then((filePath) => {
     this.state.imagePath = filePath;
     this.setState(this.state);
  });
}

...

<Image style={{width:300, height:300}} source={{uri: this.state.imagePath}} /> // important: provide style to image.

Regards

AlexDM0 avatar Mar 21 '16 15:03 AlexDM0

Why not just input base64 encoded images, it works for me :)

<Image source={{uri: "someBase64EncodedImageValue"}} />

st0ffern avatar Jun 13 '16 12:06 st0ffern

Is there a solution for this? I've only just started experimenting with this library but I have the same requirement..

dcworldwide avatar Jun 16 '16 12:06 dcworldwide

@dcworldwide Could you try:

const resolveAssetSource = require("react-native/Libraries/Image/resolveAssetSource");
const uniformValue = resolveAssetSource(require("./Image.jpg"))

st0ffern avatar Jun 16 '16 12:06 st0ffern

yes, @stoffern is right.

This used to work in previous versions of React Native, but now you will have to use resolveAssetSource to get local image you require rendered with gl-react-native.

So for now, you will have to do this way:

const resolveAssetSource = require("react-native/Libraries/Image/resolveAssetSource");
const uniformValue = resolveAssetSource(require("./Image.jpg"))

Some notes

In the short term: The next gl-react-native release (future 2.28.x) is going to provide that function to you, so later you will more reliably do: import {Surface, resolveAssetSource} from "gl-react-native". For now, use "react-native/Libraries/Image/resolveAssetSource".

In the long term: I'll try later to make this implicitly called so you can just pass-in the require(img) result again.. To do this, I need to know that your uniform is a sampler2D, and if the value is a number, then the implementation could just wrap that resolveAssetSource call for you. The only problem is I can't do it easily without some changes in the gl-react JS side implementation: It is not currently designed to know synchronously what uniform types your shader have (but only asynchronously) which is a problem as this resolve needs to happen first. So I think i'll plan a refactoring to allow this, it's not a so bad idea because then the gl-react could access the types and implement better logic / more type validation on gl-react side. Downside is the components won't React-render until the ObjC/Java has compiled the shader, but it's not really changing how it already works since shader don't render until they are compiled anyway. The tricky part is gl-react will need to work in 2-pass: one pass is to resolve all shaders you use in your tree. second pass is to do the same current logic but only if all resolve shaders are ready, otherwise, it will postpone the second pass when shaders has been loaded. Note also that this can be interrupted when componentWillReceiveProps and start that logic again.

Anyway. You don't have to read all this notes. TL.DR; it's just notes to myself when implementing this :D

gre avatar Jun 21 '16 09:06 gre

@gre I have installed gl-react & gl-react-native & rnpm link gl-react-native ,then I successfully run the project on my android machine,but something wrong with me import {Surface} from 'gl-react-native'; I can't get the Surface from the gl-react-native,and in the webstom it shows 'can't find declaration to go to' { "name": "Miracle", "version": "0.0.1", "private": true, "scripts": { "start": "node node_modules/react-native/local-cli/cli.js start" }, "dependencies": { "gl-react": "^2.2.6", "gl-react-native": "^2.32.0", "react": "^15.3.1", "react-dom": "^15.3.1", "react-native": "^0.32.0", "react-native-swipe-left": "^0.1.1", "react-native-vector-icons": "^2.1.0", "react-redux": "^4.4.5", "realm": "^0.14.3", "redux": "^3.5.2", "redux-logger": "^2.6.1", "redux-thunk": "^2.1.0" } }

import com.projectseptember.RNGL.RNGLPackage; @Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), new RNGLPackage(), new RealmReactPackage(), new VectorIconsPackage() ); }

`rootProject.name = 'Miracle'

include ':app', ':react-native-vector-icons' project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android') include ':realm' project(':realm').projectDir = new File(rootProject.projectDir, '../node_modules/realm/android') include ':gl-react-native' project(':gl-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/gl-react-native/android')`

dependencies { compile project(':gl-react-native') compile project(':realm') compile fileTree(dir: "libs", include: ["*.jar"]) compile "com.android.support:appcompat-v7:23.0.1" compile "com.facebook.react:react-native:+" // From node_modules compile project(':react-native-vector-icons') }

miracle9312 avatar Sep 08 '16 08:09 miracle9312

@miracle9312 If you post the error codes you get from the simulator or react-native it would be alot better to find the error.

st0ffern avatar Sep 08 '16 08:09 st0ffern

Hi @AlexDM0, how can i set reference for nested component inside Surface, eg: GLImage I follow your instruction but seem only work for Surface

<Surface ref={this.onRef.bind(this)} width={300} height={300} preload={true}>...</Surface>

onRef(input) {
  var path = RNFS.DocumentDirectoryPath + '/image.png';
  input.captureFrame({type:'png', format:'file', filePath: path, quality:1}).then((filePath) => {
     this.state.imagePath = filePath;
     this.setState(this.state);
  });
}

mrThinBone avatar Feb 06 '17 09:02 mrThinBone

@mrThinBone I'm not sure, I'm not using this module at the moment. I don't know if other components. Maybe someone else is better to answer this.

AlexDM0 avatar Feb 06 '17 09:02 AlexDM0