Spring 4中如何复制BitMatrix图像文件?Java/Kotlin实现咨询
Hey there! Let's clear up a key point first: BitMatrix isn't a file type—it's an in-memory data structure from the ZXing library that represents the 2D grid of a QR code (or other 2D barcode). Unlike a MultipartFile which wraps an uploaded file with an input stream, BitMatrix holds raw pixel data for the barcode, so you can't directly copy it like you would a file. Instead, you need to render it into an image format first, then write that image to your target location.
First, you'll need to render the BitMatrix into a standard image object (like BufferedImage in Java), then write that image to your desired path. Here's how to do it in both Java and Kotlin:
Java Example
import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import java.nio.file.Path; import java.nio.file.Paths; public class BitMatrixSaver { public void saveBitMatrix(BitMatrix bitMatrix, String filePath) throws Exception { // PNG is the recommended format for crisp barcodes String imageFormat = "PNG"; Path outputPath = Paths.get(filePath); // Render the BitMatrix to an image and write directly to the file path MatrixToImageWriter.writeToPath(bitMatrix, imageFormat, outputPath); } }
If you need an input stream (to replicate the MultipartFile workflow), convert the rendered image to a stream first:
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import javax.imageio.ImageIO; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; public InputStream getBitMatrixInputStream(BitMatrix bitMatrix) throws Exception { // Convert BitMatrix to a BufferedImage BufferedImage barcodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix); // Write the image to a byte array stream, then convert to an input stream ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(barcodeImage, "PNG", baos); return new ByteArrayInputStream(baos.toByteArray()); }
Kotlin Example
Kotlin uses the same ZXing utilities with more concise syntax:
import com.google.zxing.client.j2se.MatrixToImageWriter import com.google.zxing.common.BitMatrix import java.nio.file.Paths fun saveBitMatrix(bitMatrix: BitMatrix, filePath: String) { val imageFormat = "PNG" val outputPath = Paths.get(filePath) MatrixToImageWriter.writeToPath(bitMatrix, imageFormat, outputPath) }
For an input stream in Kotlin:
import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import javax.imageio.ImageIO import com.google.zxing.client.j2se.MatrixToImageWriter import com.google.zxing.common.BitMatrix fun getBitMatrixInputStream(bitMatrix: BitMatrix): ByteArrayInputStream { val barcodeImage = MatrixToImageWriter.toBufferedImage(bitMatrix) val baos = ByteArrayOutputStream() ImageIO.write(barcodeImage, "PNG", baos) return ByteArrayInputStream(baos.toByteArray()) }
inputStream for BitMatrix? BitMatrix doesn't have a built-in inputStream property because it's not a file—it's just a grid of booleans representing black/white barcode pixels. To get a stream, you have to:
- Render the BitMatrix to a
BufferedImageusing ZXing'sMatrixToImageWriter.toBufferedImage()method. - Convert that image to an input stream (as shown in the examples above).
If you want to reuse your existing Files.copy logic from the MultipartFile case, here's how to adapt it in Java:
// Assume you have your BitMatrix and target Path ready InputStream barcodeStream = getBitMatrixInputStream(bitMatrix); Files.copy(barcodeStream, this.qrcodeLocation.resolve(fileName));
内容的提问来源于stack exchange,提问作者Vaibhav Gaikwad

