I want to create a one-dimensional barcode with the values entered in the form You can create barcodes and QR codes using a library called ZXing. https://github.com/zxing/zxing
This time, it is assumed that a one-dimensional barcode (CODE_39) will be output on the detail screen.
Add ZXing library to pom.xml Specify javase when developing with Java SE Specify android for development on Android Use commons-codec for Base64 encoding separately from Zxing library
pom.xml
<dependencies>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.0</version>
</dependency>
<devendency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</devendency>
<devendency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</devendency>
</dependencies>
Prepare Entity and Repository
UsersEntity.java
@Entity
@Table(name="Users")
public class UsersEntity{
@Id
private Integer id;
private String name;
public Integer getId(){
return id;
}
public String getName(){
return name;
}
}
UsersRepository.java
import com.example.entities.UsersEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UsersRepository extends JpaRepository<UsersEntity, Integer>{
}
It is assumed that there is a list screen and a detail screen, and a barcode is output on the detail screen.
UsersController.java
Controller
public class UsersController {
@Autowired
private UsersRepository usersRepository;
@RequestMapping("/user/index")
public String index(Model model) {
List<UsersEntity> users = usersRepository.findAll();
model.addAttribute("userlist",users);
return "view/user/index";
}
@GetMapping("/user/{id}")
public String show(@PathVariable Integer id, Model model) {
UsersEntity user = usersRepository.findById(id).get();
model.addAttribute("user",user);
barcode(String.valueOf(id), model);
return "/view/user/show";
}
@GetMapping
public String barcode(@PathVariable(value="id") String id, Model model) {
try {
byte[] res = toByteArray(id);
String encodedStr64 = Base64.encodeBase64String(res);
model.addAttribute("dataUrl", encodedStr64);
} catch (Exception e) {
e.printStackTrace();
}
return "Image/data";
}
private byte[] toByteArray(String contents) throws IOException, WriterException{
BarcodeFormat format = BarcodeFormat.CODE_39;
int width = 180;
int height = 40;
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
try (ByteArrayOutputStream output = new ByteArrayOutputStream()){
Code39Writer writer = new Code39Writer();
BitMatrix bitMatrix = writer.encode(contents, format, width, height, hints);
MatrixToImageWriter.writeToStream(bitMatrix, "png", output);
return output.toByteArray();
}
}
index.html
<tr th:each="users : ${userlist}">
<td th:text="${users.id}"></td>
<td th:text="${users.name}"></td>
<td><a th:href="@{/user/{id}(id=${users.id})}">Details</a></td>
</tr>
show.html
<tr th:object="${user}">
<td th:text="*{id}"></td>
<td th:text="*{name}"></td>
<td><img th:src="|data:image/png;base64, ${dataUrl}|" class="barcode"></td>
</tr>
https://qiita.com/rubytomato@github/items/187e277b964b14f77179 https://weblabo.oscasierra.net/java-zxing-2/
Recommended Posts