JimmyDaddy / react-native-image-marker

🙈Adding text or icon watermark to your image using React Native👀👀
https://jimmydaddy.github.io/react-native-image-marker/
MIT License
316 stars 93 forks source link

App Crashing on Mark Image (android) #230

Open Muhammad19Omer opened 3 months ago

Muhammad19Omer commented 3 months ago

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):

Smartphone (please complete the following information):

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

somasekharkakarla commented 3 months ago

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

Muhammad19Omer commented 2 months ago

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 commented 2 months ago

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

anggaprytn commented 2 months ago

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.