react-native-image-marker icon indicating copy to clipboard operation
react-native-image-marker copied to clipboard

App Crashing on Mark Image (android)

Open Muhammad19Omer opened this issue 1 year ago • 5 comments

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.

Muhammad19Omer avatar Jun 12 '24 15:06 Muhammad19Omer

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

somasekharkakarla avatar Jun 13 '24 14:06 somasekharkakarla

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.

Muhammad19Omer avatar Jun 18 '24 08:06 Muhammad19Omer

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

lluisandreu avatar Jun 25 '24 07:06 lluisandreu

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.

anggaprytn avatar Jul 02 '24 10:07 anggaprytn

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,
    },

Zuxelus avatar Dec 02 '24 10:12 Zuxelus