- listar todos os qrcodes lidos obtidos invocando o serviço rest GET /qrcodes.
QRCode.java:
package org.acme.qrcodeservice;

import com.fasterxml.jackson.annotation.JsonProperty;

public class QRCode {
    @JsonProperty("data")
    private String data;

    public QRCode() {
    }

    public QRCode(String data) {
        this.data = data;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}

QRCodeService.java:
package org.acme.qrcodeservice;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import io.smallrye.mutiny.Uni;

import javax.enterprise.context.ApplicationScoped;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@ApplicationScoped
public class QRCodeService {

    private final String storagePath = "qrcodes/";

    public Uni<String> createQRCode(String data, int width, int height) {
        String imageFormat = "png";
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix;
        try {
            bitMatrix = qrCodeWriter.encode(data, BarcodeFormat.QR_CODE, width, height);
        } catch (WriterException e) {
            return Uni.createFrom().failure(e);
        }

        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        try {
            MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, pngOutputStream);
        } catch (IOException e) {
            return Uni.createFrom().failure(e);
        }

        byte[] pngData = pngOutputStream.toByteArray();
        String fileName = System.currentTimeMillis() + ".png";
        try {
            Path path = Paths.get(storagePath + fileName);
            Files.write(path, pngData);
            return Uni.createFrom().item(fileName);
        } catch (IOException e) {
            return Uni.createFrom().failure(e);
        }
    }

    public Uni<List<String>> getAllQRCodeFiles() {
        try {
            List<String> fileNames = new ArrayList<>();
            Files.list(Paths.get(storagePath))
                    .filter(Files::isRegularFile)
                    .forEach(file -> fileNames.add(file.getFileName().toString()));
            return Uni.createFrom().item(fileNames);
        } catch (IOException e) {
            return Uni.createFrom().failure(e);
        }
    }

    /*public static byte[] getQRCode(String text, int width, int height)
        throws WriterException, IOException {
        String imageFormat = "png"; // could be "gif", "tiff", "jpeg"
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.
                encode(text, BarcodeFormat.QR_CODE, width, height);
        ByteArrayOutputStream pngOutputStream =
                new ByteArrayOutputStream();
        MatrixToImageWriter.
                writeToStream(bitMatrix, imageFormat, pngOutputStream);
        byte[] pngData = pngOutputStream.toByteArray();
        return pngData;
    }*/
}

QRCodeResource.java:
package org.acme.resource;

import com.google.zxing.WriterException;
import org.acme.qrcodeservice.QRCode;
import org.acme.qrcodeservice.QRCodeService;

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.List;


@Path("/qrcodes")
public class QRCodeResource {

    @Inject
    QRCodeService qrCodeService;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response generateQRCode(QRCode qrCode) {
        qrCodeService.createQRCode(qrCode.getData(), 400, 400)
                .subscribe().with(
                        fileName -> System.out.println("QR code saved as: " + fileName),
                        failure -> System.err.println("Failed to create QR code: " + failure.getMessage())
                );
        return Response.status(Response.Status.CREATED).build();
    }

    @GET
    @Produces("application/json")
    public Response getAllQRCodeFiles() {
        List<String> fileNames = qrCodeService.getAllQRCodeFiles()
                .await().indefinitely();
        return Response.ok(fileNames).build();
    }


}

App.js:
import React, { useState, useRef } from 'react';
import { View, Text, TouchableOpacity, Linking } from 'react-native';
import { RNCamera } from 'react-native-camera';
import QRCodeScanner from 'react-native-qrcode-scanner';

const App = () => {
  const qrcodeRef = useRef(null)
  const [link, setLink] = useState("")

  const handleLink = () => {
      // Send the scanned data to the backend
      if (link) {
        fetch('http://192.168.1.67:8080/qrcodes', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ data: link }),
      })
        .then(response => {
          if (response.ok) {
            console.log('Data sent to backend successfully');
          } else {
            console.log('Failed to send data to backend');
          }
        })
        .catch(error => {
          console.error('Error sending data to backend:', error);
        });
      }
      
    // Clear the link after processing
    setLink("");
  
    // Reactivate the QR code scanner
    qrcodeRef.current.reactivate()
  };

  return (
    <QRCodeScanner
      ref={qrcodeRef}
      onRead={({ data }) => setLink(data)} // Update the link state when QR code is read
      flasMode={RNCamera.Constants.FlashMode.off}
      topContent={
        <View>
          <Text>{link}</Text>
        </View>
      }
      bottomContent={
        <View>
          <TouchableOpacity 
            onPress={handleLink}
            style={{ padding: 12, backgroundColor: "#0277BD", marginTop: 20 }}>
            <Text style={{ color: "#FFFFFF" }}>Process QR Code</Text>
          </TouchableOpacity>
        </View>
      }    
    />
  );
};

export default App;