Java - QrCode Obter retorno em bytes ou em base64

Alguém sabe como faço pra obter o retorno em bytes ou em base64 ?

package br.com.qrcode;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QrCodeSwing {

	// Function to create the QR code
	public static void createQR(String data, String path,
								String charset, Map hashMap,
								int height, int width)
		throws WriterException, IOException
	{

		BitMatrix matrix = new MultiFormatWriter().encode(
			new String(data.getBytes(charset), charset),
			BarcodeFormat.QR_CODE, width, height);

		MatrixToImageWriter.writeToFile(
			matrix,
			path.substring(path.lastIndexOf('.') + 1),
			new File(path));
	}

	// Driver code
	public static void main(String[] args)
		throws WriterException, IOException,
			NotFoundException
	{

		// The data that the QR code will contain
		String data = "www.google.com.br";

		// The path where the image will get saved
		String path = "C:\\Users\\cleit\\OneDrive\\Imagens\\Saved Pictures\\demo.png";

		// Encoding charset
		String charset = "UTF-8";

		Map<EncodeHintType, ErrorCorrectionLevel> hashMap
			= new HashMap<EncodeHintType,
						ErrorCorrectionLevel>();

		hashMap.put(EncodeHintType.ERROR_CORRECTION,
					ErrorCorrectionLevel.L);

        
		//createQR(data, path, charset, hashMap, 200, 200);
		System.out.println("QR Code Generated!!! ");
	}
}

Vc pode ler os bytes de um arquivo fora do classpath assim:

String pathImage = "/caminho/absoluto/da/imagem/icone.png";
Path path = Paths.get(pathImage);
byte[] bytes = Files.readAllBytes(path);

O método readAllBytes exige que vc trate possíveis erros de IOException (try ... catch).

1 curtida