Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App Crashing on Mark Image (android) #230

Open
Muhammad19Omer opened this issue Jun 12, 2024 · 5 comments · May be fixed by #240
Open

App Crashing on Mark Image (android) #230

Muhammad19Omer opened this issue Jun 12, 2024 · 5 comments · May be fixed by #240

Comments

@Muhammad19Omer
Copy link

Describe the bug
The app crashes (only on android) when the function Marker.markImage is called.

To Reproduce
Steps to reproduce the behavior:
const markedPhoto = await Marker.markImage({
saveFormat: ImageFormat.png,
backgroundImage: {
src: media.path,
scale: 1,
},
watermarkImages: [
{
src: logo,
position: {
position: Position.topLeft,
},
alpha: 0.5,
scale: 1.2
},
{
src: logo,
position: {
position: Position.bottomRight,
},
alpha: 0.5,
scale: 1.2
},
],
});

where media is the PhotoFile that is being marked.

Expected behavior
A marked image with the logo added to the top left and bottom right of the image.

Screenshots

Devlopment environment(please complete the following information):

  • OS: OSX
  • nodejs: v21.6.2
  • react-native: 0.73.8
  • react-native-image-marker : 1.2.6

Smartphone (please complete the following information):

  • Device: Android Emulator
  • OS: Android VanillaIceCream

Additional context
This only happens on Android, not on iOS.

@somasekharkakarla
Copy link

@Muhammad19Omer did you find anything. i am also facing same. i am error like Error:null. Not much info.
Team please help.

@Muhammad19Omer
Copy link
Author

Hey @somasekharkakarla , unfortunately not. I’ve tried making a similar functionality by styling a watermark using an Image Background, and then using a View Shot to capture the View as a png, but I would really appreciate it if this bug gets fixed so I can just use the simpler solution.

@lluisandreu
Copy link

I have the same issue on Android using SDK 51. I would have to look for alternatives.

@anggaprytn
Copy link

anggaprytn commented Jul 2, 2024

SOLVED

Issue: Base64 Image Not Working on Android

I encountered an issue where using a Base64 image directly in the ImageMarker configuration was causing an error on Android, although it worked fine on iOS.

Solution

I solved this issue by converting the Base64 string to a temporary file and then using the file path as the image source. This approach mimics the behavior of using an HTTP URL.

Example Code

Here is the example code that demonstrates the solution:

import Fs from 'react-native-fs';

if (cameraRef.current) {
  const options = { quality: 0.6, base64: false };
  const data = await cameraRef.current.takePictureAsync(options);
  console.log('data', data);

  const { uri } = await ImageResizer.createResizedImage(
    data.uri,
    640,
    640,
    'JPEG',
    60
  );
  console.log('data.uri', data.uri);
  console.log('uri', uri);

  Fs.unlink(data.uri);
  const selfie = await Fs.readFile(uri, 'base64');
  Fs.unlink(uri);

  const tempFilePath = `${Fs.TemporaryDirectoryPath}/selfie.jpg`;
  await Fs.writeFile(tempFilePath, selfie, 'base64');

  try {
    const path = await ImageMarker.markImage({
      backgroundImage: {
        src: { uri: `file://${tempFilePath}` },
        scale: 1,
      },
      watermarkImages: [
        {
          src: require('@/assets/images/watermark.png'),
          scale: 0.5,
          position: {
            position: Position.bottomLeft,
          },
        },
      ],
      quality: 100,
      saveFormat: ImageFormat.base64,
    });

    console.log(path);
    setUriTest(path);
  } catch (err) {
    console.log('error marking img: ', err);
  }
}

Explanation

  1. Take a Picture: Capture an image using the camera.
  2. Resize Image: Resize the captured image.
  3. Convert to Base64: Read the resized image as a Base64 string.
  4. Save as Temporary File: Write the Base64 string to a temporary file.
  5. Use Temporary File Path: Use the temporary file path in the ImageMarker configuration.

This workaround resolves the issue with using Base64 images directly on Android by converting them into a format that is compatible with the image processing libraries.

@Zuxelus
Copy link

Zuxelus commented Dec 2, 2024

The problem is still here.
Android
"react-native": "0.76.3",
"react-native-image-marker": "1.2.6",

Was working in old versions like "1.1.11". Not working in 1.2.6

    backgroundImage: {
      src: { uri: `data:${image.mime};base64,${image.base64}` },
      scale: 1,
    },

Now only

  const tempFilePath = `${fs.TemporaryDirectoryPath}/temp.jpg`;
  await fs.writeFile(tempFilePath, image.base64, 'base64');

  return {
    backgroundImage: {
      src: { uri: `file://${tempFilePath}` },
      scale: 1,
    },

@alexisrougnant alexisrougnant linked a pull request Dec 13, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants