Jak dodać podpisy cyfrowe do plików w Javie przy użyciu kodów kreskowych i kodów QR
Wprowadzenie
Zastanawiałeś się kiedyś, jak udowodnić, że Twoje pliki nie zostały zmodyfikowane przy użyciu digital signature java? Albo potrzebowałeś sposobu na programowe uwierzytelnianie dokumentów bez skomplikowanych konfiguracji kryptograficznych? Tradycyjne podpisy cyfrowe mogą być przesadą w niektórych przypadkach. Czasami potrzebna jest lekka, skanowalna metoda weryfikacji integralności pliku — szczególnie przy pracy z archiwami, kopiami zapasowymi lub zautomatyzowanymi przepływami pracy. Właśnie tutaj wchodzą w grę podpisy w postaci kodów kreskowych i kodów QR.
W tym samouczku dowiesz się, jak wdrożyć podpisy cyfrowe w Javie przy użyciu GroupDocs.Signature. Skoncentrujemy się na podpisywaniu archiwów TAR (idealnych dla systemów backupowych i dystrybucji oprogramowania), ale techniki te działają z różnymi formatami dokumentów. Niezależnie od tego, czy tworzysz system zarządzania dokumentami, czy po prostu chcesz dodać dodatkową warstwę bezpieczeństwa do swoich plików, jesteś we właściwym miejscu.
Co zdobędziesz po zakończeniu:
- Działającą implementację podpisów kodów kreskowych i QR w Javie
- Zrozumienie, kiedy używać każdego typu podpisu (i dlaczego ma to znaczenie)
- Praktyczne rozwiązania typowych wyzwań związanych z podpisywaniem
- Wzorce integracji, które możesz wykorzystać już dziś
- Wskazówki optymalizacji wydajności dla systemów produkcyjnych
Zanurzmy się — nie potrzebujesz stopnia z kryptografii.
Szybkie odpowiedzi
- Jaką bibliotekę obsługuje podpisy kodów kreskowych w Javie? GroupDocs.Signature for Java.
- Który typ podpisu przechowuje więcej danych? QR codes (up to 4,296 alphanumeric characters).
- Czy mogę podpisać duże pliki TAR (>100 MB)? Yes—use background threads and increase JVM heap.
- Czy potrzebne jest połączenie z internetem? No, the library works completely offline.
- Czy wymagana jest licencja do produkcji? Yes, a valid GroupDocs.Signature license is mandatory.
Co to jest Digital Signature Java?
Digital signature java to proces osadzania weryfikowalnego tokenu wizualnego — takiego jak kod kreskowy lub kod QR — bezpośrednio w pliku generowanym w Javie, aby potwierdzić jego autentyczność i integralność. Dzięki dołączeniu tego tokenu wizualnego, programiści mogą zapewnić szybki, czytelny dla człowieka sposób potwierdzenia, że plik nie został zmieniony od momentu podpisania, jednocześnie umożliwiając programową weryfikację poprzez API GroupDocs.Signature.
Dlaczego używać podpisów kodów kreskowych lub QR?
GroupDocs.Signature obsługuje 50+ input and output formats (including PDF, DOCX, XLSX, HTML, PNG, and TAR) i może przetwarzać dokumenty wielostronicowe bez ładowania całego pliku do pamięci. Kody kreskowe i QR dają skanowalny, samodzielny dowód autentyczności, eliminując potrzebę zewnętrznych urzędów certyfikacji w wielu wewnętrznych przepływach pracy.
Wymagania wstępne
- GroupDocs.Signature for Java Library – wersja 23.12 lub nowsza
- Java Development Kit (JDK) – wersja 8 lub wyższa
- IDE – IntelliJ IDEA, Eclipse lub dowolny edytor kompatybilny z Javą
- Podstawowa znajomość Javy – powinieneś być zaznajomiony z klasami i importami
Konfiguracja środowiska
Uzyskanie GroupDocs.Signature w projekcie jest proste. Wybierz narzędzie budowania:
Maven (add this to your pom.xml):
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-signature</artifactId>
<version>23.12</version>
</dependency>
Gradle (add to your build.gradle):
implementation 'com.groupdocs:groupdocs-signature:23.12'
Manual Download: Not using Maven or Gradle? Grab the JAR directly from GroupDocs.Signature releases and add it to your classpath.
Uzyskanie licencji
GroupDocs offers flexible licensing:
- Free Trial: Perfect for testing—no credit card required. Start here
- Temporary License: Need more time to evaluate? Request a temporary license for full‑feature access during development
- Production License: When you’re ready to deploy, purchase a license based on your needs
Additional useful links
- Dokumentacja GroupDocs.Signature for Java
- API Reference Guide
- Community Support Forum
- Latest Library Releases
- Free Trial Download
- Request Temporary License
- Purchase Full License
Pro tip: Start with the free trial to prototype your solution, then grab a temporary license if you need more time before committing.
Konfiguracja GroupDocs.Signature dla Javy
Klasa Signature jest punktem wejścia dla wszystkich operacji podpisywania w GroupDocs.Signature. Reprezentuje pojedynczy plik załadowany do pamięci i udostępnia metody do dodawania, wyszukiwania lub usuwania podpisów wizualnych.
Utwórz instancję Signature wskazującą na Twój plik TAR. To ładuje plik do pamięci w celu przetworzenia:
import com.groupdocs.signature.Signature;
public class InitializeSignature {
public static void main(String[] args) {
Signature signature = new Signature("your-document-path");
// Additional setup and usage here...
}
}
Important: Always close the Signature object when you’re done (or use try‑with‑resources) to avoid memory leaks with large files.
Wybór między podpisami kodu kreskowego a kodu QR
Nie jesteś pewien, którego typu podpisu użyć? Oto szybki przewodnik decyzyjny:
| Czynnik | Kod kreskowy (Code128) | Kod QR |
|---|---|---|
| Pojemność danych | ~80 znaków | Do 4 296 znaków alfanumerycznych |
| Czytelność | Wymaga skanera kodów kreskowych | Działa z aparatami smartfonów |
| Wydajność przestrzeni | Bardziej zwarty w poziomie | Wymaga kwadratowego obszaru |
| Najlepsze dla | Proste identyfikatory, znaczniki czasu, krótkie kody | Adresy URL, dane JSON, szczegółowe metadane |
| Korekcja błędów | Minimalna | Wbudowana (może odzyskać po uszkodzeniu) |
Reguła ogólna:
- Używaj kodów kreskowych do szybkich, skanowalnych identyfikatorów lub znaczników czasu.
- Używaj kodów QR, gdy potrzebujesz osadzić bogatsze dane lub chcesz kompatybilności ze smartfonami.
- Połącz oba dla maksymalnej redundancji i audytowalności.
Przewodnik implementacji
Sign TAR Archive with Barcode
Dlaczego podpisywać kodami kreskowymi?
Kody kreskowe są idealne dla archiwów TAR, ponieważ są kompaktowe i skanowalne. Możesz osadzać znaczniki czasu, numery wersji, identyfikatory użytkowników lub wartości sum kontrolnych dla szybkiej weryfikacji.
Kroki
1. Inicjalizacja Signature
First, create a Signature instance for the TAR file:
import com.groupdocs.signature.Signature;
final Signature signature = new Signature("path/to/your/archive.tar");
Pro tip: For large TAR files (over 100 MB), run the signing operation in a background thread to keep the UI responsive.
2. Konfiguracja opcji kodu kreskowego
The BarcodeSignature class defines the barcode content, type, and placement. The BarcodeOptions object holds these settings:
import com.groupdocs.signature.options.sign.BarcodeSignOptions;
import com.groupdocs.signature.domain.barcodes.BarcodeTypes;
BarcodeSignOptions bcOptions = new BarcodeSignOptions("12345678", BarcodeTypes.Code128);
bcOptions.setLeft(100); // X position in pixels
bcOptions.setTop(100); // Y position in pixels
BarcodeOptions lets you specify the visual appearance and position of the barcode.BarcodeTypes is an enum that lists supported barcode symbologies such as Code128, Code39, etc.
What’s happening here?
"12345678"is the data encoded in the barcode—replace it with your actual ID, timestamp, or verification code.BarcodeTypes.Code128balances data capacity with scan reliability.- Position values (100, 100) place the barcode 100 px from the top‑left corner.
Customization options you might want:
bcOptions.setWidth(200); // Barcode width in pixels
bcOptions.setHeight(50); // Barcode height in pixels
bcOptions.setForeColor(Color.BLACK); // Barcode color
bcOptions.setBackgroundColor(Color.WHITE); // Background color
3. Podpisz i zapisz dokument
Execute the signing operation and store the signed archive:
import com.groupdocs.signature.domain.SignResult;
String outputFilePath = "output/path/SignWithBarcode/archive_signed.tar";
SignResult signResult = signature.sign(outputFilePath, bcOptions);
The returned SignResult object tells you whether the operation succeeded and where the signature was placed.
Common gotcha: Ensure the output directory exists before calling sign(). The library won’t create parent directories automatically.
Sign TAR Archive with QR Code
Kiedy używać kodów QR
QR codes shine when you need to store structured data (JSON, XML), embed verification URLs, or enable smartphone scanning.
Kroki
1. Initialise Signature
Same as before—create your Signature instance:
final Signature signature = new Signature("path/to/your/archive.tar");
2. Konfiguracja opcji kodu QR
Set up your QR code with the data you want to embed:
import com.groupdocs.signature.options.sign.QrCodeSignOptions;
import com.groupdocs.signature.domain.qrcodes.QrCodeTypes;
QrCodeSignOptions qrOptions = new QrCodeSignOptions("12345678", QrCodeTypes.QR);
qrOptions.setLeft(400); // X position
qrOptions.setTop(400); // Y position
QrCodeTypes is an enum that specifies the type of QR code to generate (standard QR, DataMatrix, Aztec, etc.).
Real‑world example – embed a JSON payload with verification data:
String verificationData = "{\"version\":\"1.0\",\"timestamp\":\"2025-01-02T10:30:00Z\",\"user\":\"john.doe\"}";
QrCodeSignOptions qrOptions = new QrCodeSignOptions(verificationData, QrCodeTypes.QR);
QR Code type options:
QrCodeTypes.QR– standardowy kod QR (najczęstszy)QrCodeTypes.DataMatrix– bardziej zwarty dla małych danychQrCodeTypes.Aztec– dobry na powierzchnie zakrzywione
3. Podpisz i zapisz dokument
Complete the signing process just like with barcodes:
String outputFilePath = "output/path/SignWithQRCode/archive_signed.tar";
SignResult signResult = signature.sign(outputFilePath, qrOptions);
Performance note: QR code generation is slightly slower than barcodes due to error‑correction calculations, but the difference is negligible for most use cases (typically a few milliseconds).
Sign TAR Archive with Multiple Signatures
Dlaczego używać wielu podpisów?
- Redundancja — jeśli jeden podpis zostanie uszkodzony, drugi nadal może zweryfikować.
- Różne grupy odbiorców — kody kreskowe dla skanerów, kody QR dla smartfonów.
- Dane warstwowe — szybki identyfikator w kodzie kreskowym, szczegółowe metadane w kodzie QR.
- Zgodność — niektóre regulacje wymagają wielu metod weryfikacji.
Kroki
1. Inicjalizacja Signature
Same initialisation as before:
final Signature signature = new Signature("path/to/your/archive.tar");
2. Konfiguracja wielu opcji
Create both signature types and combine them in a list:
import java.util.ArrayList;
import java.util.List;
// Set up barcode (reusing from earlier example)
BarcodeSignOptions bcOptions = new BarcodeSignOptions("12345678", BarcodeTypes.Code128);
bcOptions.setLeft(100);
bcOptions.setTop(100);
// Set up QR code (different position to avoid overlap)
QrCodeSignOptions qrOptions = new QrCodeSignOptions("12345678", QrCodeTypes.QR);
qrOptions.setLeft(400);
qrOptions.setTop(400);
// Combine them
List<com.groupdocs.signature.options.sign.SignOptions> listOptions = new ArrayList<>();
listOptions.add(bcOptions);
listOptions.add(qrOptions);
Pro tip: Position signatures strategically—corners or non‑interfering areas work best for TAR archives.
3. Podpisz i zapisz dokument
Pass the list of options to the sign() method:
String outputFilePath = "output/path/SignWithMultipleSignatures/archive_signed.tar";
SignResult signResult = signature.sign(outputFilePath, listOptions);
GroupDocs processes each signature sequentially, embedding them into the document metadata. The order in your list does not affect verification.
Przykłady zastosowań w rzeczywistym świecie
1. Pipeline dystrybucji oprogramowania
Scenario: Distributing software packages as TAR archives and proving they haven’t been modified.
Solution: Sign each release with a QR code containing a JSON payload:
String releaseData = String.format(
"{\"version\":\"%s\",\"buildDate\":\"%s\",\"sha256\":\"%s\"}",
version, buildDate, checksum
);
Why it works: Users can scan the QR code to verify package integrity before installation—no need for GPG key management.
2. Zautomatyzowane systemy backupu
Scenario: Daily backup TAR archives need audit trails.
Solution: Add a barcode with the backup timestamp and server ID:
String backupId = String.format("SRV01-%s", LocalDateTime.now().format(formatter));
BarcodeSignOptions bcOptions = new BarcodeSignOptions(backupId, BarcodeTypes.Code128);
Why it works: Quick visual verification of backup authenticity without opening the archive.
3. Systemy zarządzania dokumentami
Scenario: Legal documents stored as archives require tamper‑proof verification.
Solution: Use both barcode (quick scan) and QR code (detailed metadata) on the same archive.
4. Śledzenie łańcucha dostaw
Scenario: Tracking file packages through multiple organisations.
Solution: Embed QR codes with tracking URLs that link to a verification API:
String trackingUrl = "https://verify.yourcompany.com/track/" + uniqueId;
QrCodeSignOptions qrOptions = new QrCodeSignOptions(trackingUrl, QrCodeTypes.QR);
Typowe problemy i rozwiązania
Problem 1: „Podpis nie znaleziony” po podpisaniu
Symptom: sign() succeeds, but the signature isn’t visible.
Causes: Wrong placement, overwriting original file, TAR viewer limitations.
Solution:
// Always verify the signing succeeded
SignResult result = signature.sign(outputFilePath, bcOptions);
if (result.getSucceeded().size() > 0) {
System.out.println("Signature added successfully at: " + outputFilePath);
} else {
System.err.println("Signing failed: " + result.getFailed());
}
// Use absolute paths to avoid confusion
String absolutePath = new File(outputFilePath).getAbsolutePath();
Problem 2: OutOfMemoryError przy dużych plikach TAR
Symptom: JVM crashes for archives > 500 MB.
Solution: Increase heap size (-Xmx) and dispose of Signature objects promptly:
java -Xmx2G -jar your-application.jar
Or implement chunked processing:
// For very large files, consider signing metadata separately
// rather than embedding in the TAR itself
Problem 3: Dane podpisu są obcinane
Symptom: Long strings are cut off.
Cause: Exceeded capacity of Code128 (≈ 80 chars).
Solution: Switch to QR codes for longer payloads:
// Bad: Too much data for Code128
BarcodeSignOptions bcOptions = new BarcodeSignOptions(veryLongString, BarcodeTypes.Code128);
// Good: Use QR code instead
QrCodeSignOptions qrOptions = new QrCodeSignOptions(veryLongString, QrCodeTypes.QR);
Problem 4: Błędy walidacji licencji
Symptom: LicenseException or “Trial version” warnings in production.
Solution: Load the license before creating any Signature instances:
import com.groupdocs.signature.License;
License license = new License();
license.setLicense("path/to/GroupDocs.Signature.lic");
// Now create signatures
Signature signature = new Signature("document.tar");
Pro tip: Load the license once at application startup, not before every signing operation.
Problem 5: Wartości pozycji nie działają zgodnie z oczekiwaniami
Symptom: Signatures appear in unexpected locations.
Cause: Confusion between pixels and points.
Solution: GroupDocs uses pixels by default. For precise placement:
bcOptions.setLeft(100); // 100 pixels from left edge
bcOptions.setTop(100); // 100 pixels from top edge
// If you need percentage-based positioning:
bcOptions.setHorizontalAlignment(HorizontalAlignment.Center);
bcOptions.setVerticalAlignment(VerticalAlignment.Center);
Wzorce integracji
Wzorzec 1: Usługa REST API
Expose signing as a microservice:
@RestController
@RequestMapping("/api/signature")
public class SignatureController {
@PostMapping("/sign")
public ResponseEntity<SignatureResponse> signFile(
@RequestParam("file") MultipartFile file,
@RequestParam("signatureType") String type) {
try {
// Save uploaded file temporarily
File tempFile = File.createTempFile("upload-", ".tar");
file.transferTo(tempFile);
// Sign based on type
Signature signature = new Signature(tempFile.getAbsolutePath());
SignOptions options = type.equals("barcode")
? createBarcodeOptions()
: createQROptions();
String outputPath = generateOutputPath();
SignResult result = signature.sign(outputPath, options);
// Return signed file
return ResponseEntity.ok(new SignatureResponse(outputPath, result));
} catch (Exception e) {
return ResponseEntity.status(500).body(null);
}
}
}
Wzorzec 2: Pipeline przetwarzania wsadowego
Sign multiple archives in a pipeline:
public class BatchSigner {
public void signArchiveBatch(List<File> archives) {
ExecutorService executor = Executors.newFixedThreadPool(4);
archives.forEach(archive -> {
executor.submit(() -> {
try {
signSingleArchive(archive);
} catch (Exception e) {
logger.error("Failed to sign: " + archive.getName(), e);
}
});
});
executor.shutdown();
executor.awaitTermination(1, TimeUnit.HOURS);
}
private void signSingleArchive(File archive) throws Exception {
Signature signature = new Signature(archive.getAbsolutePath());
// ... signing logic
}
}
Wzorzec 3: Architektura zdarzeniowa
Trigger signing when archives are created:
@Component
public class ArchiveCreatedListener {
@EventListener
public void onArchiveCreated(ArchiveCreatedEvent event) {
CompletableFuture.runAsync(() -> {
signArchive(event.getFilePath());
});
}
private void signArchive(String filePath) {
// ... signing logic
}
}
Rozważania dotyczące wydajności
Zarządzanie pamięcią
The problem: Each Signature instance loads the full file into memory.
Best practices:
// Bad: Creating multiple instances for same file
Signature sig1 = new Signature("file.tar");
Signature sig2 = new Signature("file.tar"); // Loads again!
// Good: Reuse the instance
try (Signature signature = new Signature("file.tar")) {
signature.sign(output1, options1);
signature.sign(output2, options2); // Same instance, different outputs
}
Optymalizacja rozmiaru pliku
- Małe pliki (< 10 MB) — podpisuj synchronicznie.
- Średnie pliki (10‑100 MB) — używaj wątków w tle.
- Duże pliki (> 100 MB) — rozważ podpisywanie metadanych osobno lub użycie API strumieniowego.
Złożoność podpisu (przybliżone czasy na standardowym serwerze)
| Typ podpisu | Czas na dokument |
|---|---|
| Pojedynczy kod kreskowy | 50‑100 ms |
| Pojedynczy kod QR | 100‑200 ms |
| Wiele podpisów | 150‑300 ms |
Optimization tip: For thousands of files, batch them and use a thread pool (see the batch processing pattern above).
Aktualizacje biblioteki
GroupDocs releases regular performance improvements. Always check the changelog before major deployments.
Update strategy:
- Test new versions in staging.
- Review breaking changes.
- Benchmark with real files.
- Roll out incrementally.
Najlepsze praktyki dla produkcji
1. Sprawdź status licencji
License license = new License();
if (!license.isLicensed()) {
logger.warn("Running in trial mode - features may be limited");
}
2. Wdrożenie solidnej obsługi błędów
try {
signature.sign(outputPath, options);
} catch (Exception e) {
logger.error("Signature failed", e);
// Don't just swallow exceptions - log or re-throw
throw new SignatureException("Failed to sign document", e);
}
3. Używaj opisowych danych podpisu
// Bad: Meaningless ID
new BarcodeSignOptions("12345678", BarcodeTypes.Code128);
// Good: Self-documenting data
String signatureData = String.format("DOC-%s-%s",
docType,
LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME)
);
new BarcodeSignOptions(signatureData, BarcodeTypes.Code128);
4. Wersjonuj format podpisu
Include a version number in embedded JSON to future‑proof your verification logic:
String qrData = String.format(
"{\"v\":\"1.0\",\"type\":\"archive\",\"timestamp\":\"%s\"}",
timestamp
);
5. Testuj z rzeczywistymi plikami – zawsze waliduj na archiwach o rozmiarach produkcyjnych, aby wcześnie wykryć problemy z pamięcią i wydajnością.
Zakończenie
You’ve now got a solid foundation for implementing digital signature java using barcodes and QR codes. Here’s what you learned:
- How to sign TAR archives (and other documents) with both barcode and QR code signatures
- When to choose each signature type based on specific needs
- How to troubleshoot common issues before they hit production
- Real‑world integration patterns for REST APIs, batch processing, and event‑driven systems
- Performance optimisation techniques for handling files of any size
Next steps:
- Explore signature verification with the
search()method. - Try other document formats—GroupDocs.Signature supports PDF, DOCX, XLSX, PNG, and more.
- customise signature appearance (colors, sizes, borders).
- Build a verification API to validate signatures programmatically.
The power of GroupDocs.Signature goes far beyond this guide. Check out the full documentation to discover advanced features like text signatures, image signatures, and metadata extraction.
Have questions or want to share your implementation? Join the GroupDocs community forums for help from other developers.
Najczęściej zadawane pytania
Q: Czy mogę podpisywać dokumenty inne niż archiwa TAR?
A: Absolutely! GroupDocs.Signature supports over 50 file formats, including PDF, DOCX, XLSX, PNG, and more. Change only the file extension in the Signature constructor to work with any supported type.
Q: Jak zweryfikować podpisy po ich utworzeniu?
A: Use the search() method to locate and validate signatures:
Signature signature = new Signature("signed-document.tar");
BarcodeSearchOptions searchOptions = new BarcodeSearchOptions();
List<BarcodeSignature> signatures = signature.search(BarcodeSignature.class, searchOptions);
Q: Czy podpisy są bezpieczne przed manipulacją?
A: Barcode and QR code signatures provide visual verification but are not cryptographically strong like digital certificates. For maximum security, combine them with traditional PKI or store signature hashes in an external database.
Q: Jakie jest maksymalne ilość danych, które mogę przechowywać w podpisie?
- Kod kreskowy Code128: ~80 znaków alfanumerycznych
- Kod QR (Wersja 40): do 4 296 znaków alfanumerycznych lub 7 089 znaków numerycznych
Q: Czy mogę dostosować wygląd podpisu?
A: Yes! Control colours, sizes, borders, and more:
bcOptions.setForeColor(Color.BLUE);
bcOptions.setBackgroundColor(Color.YELLOW);
bcOptions.setBorder(new Border());
bcOptions.getBorder().setColor(Color.RED);
bcOptions.getBorder().setWeight(2);
Q: Co się stanie, jeśli podpiszę plik dwukrotnie?
A: Each sign() call adds a new signature. To replace an existing one, delete it first with the delete() method.
Q: Jak obsługiwać duże pliki, nie wyczerpując pamięci?
A: Increase JVM heap (-Xmx), dispose of Signature objects promptly, and consider signing metadata separately for multi‑gigabyte archives.
Q: Czy potrzebuję połączenia z internetem, aby podpisywać dokumenty?
A: No. GroupDocs.Signature works entirely offline once the library is installed.
Last Updated: 2026-05-21
Testowano z: GroupDocs.Signature 23.12 for Java
Autor: GroupDocs