Skip to content

Commit

Permalink
use screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
sukso96100 committed Jul 13, 2024
1 parent 69ed52f commit ab9d617
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 26 deletions.
20 changes: 20 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:ubuntu_kr_qr_kiosk/printScreen.dart';
import 'checkInByEmailScreen.dart';
import 'checkInByBarcodeScreen.dart';
import 'package:yaru/yaru.dart';
Expand Down Expand Up @@ -120,6 +121,25 @@ class _KioskMainPageState extends State<KioskMainPage> {
);
},
),
IconButton(
icon: Icon(Icons.print),
tooltip: "인쇄 테스트",
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PrintPage(
nametagData: NametagData(
"[이름]",
"[소속]",
"[직책]",
"https://ubuntu-kr.org",
// "교환권\n티셔츠 XL (테스트)\n 도시락/채식 (테스트)",
"",
true))),
);
},
)
]),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
Expand Down
36 changes: 15 additions & 21 deletions lib/printScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import 'package:qr_flutter/qr_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'imgutil.dart';
import 'printclient.dart';

GlobalKey nametagKey = GlobalKey();
GlobalKey couponKey = GlobalKey();
import 'package:screenshot/screenshot.dart';

class NametagData {
final String name;
Expand All @@ -34,6 +32,8 @@ class PrintPage extends StatefulWidget {
}

class _PrintPageState extends State<PrintPage> {
ScreenshotController nametagScreenshotController = ScreenshotController();
ScreenshotController couponScreenshotController = ScreenshotController();
var qrCodeContent = "";
var nametagName = "";
var nametagAffiliation = "";
Expand All @@ -58,8 +58,7 @@ class _PrintPageState extends State<PrintPage> {
@override
void initState() {
super.initState();
Timer(Duration(seconds: 2), () async {
var prefs = await SharedPreferences.getInstance();
var prefs = SharedPreferences.getInstance().then((prefs) {
var widthMm = prefs.getInt('printCanvasWidthMm') ?? 70;
var heightMm = prefs.getInt('printCanvasHeightMm') ?? 70;
var canvasDpi = prefs.getInt('printCanvasDpi') ?? 203;
Expand All @@ -69,7 +68,9 @@ class _PrintPageState extends State<PrintPage> {
canvasWidthPx = (widthMm * canvasDpi) / 25.4;
canvasHeightPx = (heightMm * canvasDpi) / 25.4;
});
var result1 = await printNametag(nametagKey);
});
Timer(Duration(seconds: 1), () async {
var result1 = await printNametag(nametagScreenshotController);
var result1Msg = result1
? "명찰 인쇄 완료. Nametag has been printed."
: "명찰 인쇄중 오류 발생. Error while printing nametag.";
Expand All @@ -79,7 +80,7 @@ class _PrintPageState extends State<PrintPage> {
ScaffoldMessenger.of(context).showSnackBar(snackBar);
await Future.delayed(Duration(seconds: 1));
if (couponDetail != "") {
var result2 = await printNametag(couponKey);
var result2 = await printNametag(couponScreenshotController);
var result2Msg = result2
? "교환권 인쇄 완료. Coupon has been printed."
: "교환권 인쇄중 오류 발생. Error while printing Coupon.";
Expand All @@ -102,17 +103,10 @@ class _PrintPageState extends State<PrintPage> {
print('initState is called');
}

Future<ui.Image> _capturePng(GlobalKey globalKey) async {
final RenderRepaintBoundary boundary =
globalKey.currentContext!.findRenderObject()! as RenderRepaintBoundary;
final ui.Image image = await boundary.toImage();
return image;
}

Future<bool> printNametag(GlobalKey globalKey) async {
var uiImage = await _capturePng(globalKey);
Future<bool> printNametag(ScreenshotController controller) async {
var uint8Image = await controller.capture();
var result =
await printImageToLabel(uiImage, printerVendorId, printerProductId);
await printImageToLabel(uint8Image!, printerVendorId, printerProductId);
setState(() {
isProcessingQrCheckin = false;
});
Expand All @@ -137,8 +131,8 @@ class _PrintPageState extends State<PrintPage> {
color: Colors.black,
),
),
child: RepaintBoundary(
key: nametagKey,
child: Screenshot(
controller: nametagScreenshotController,
child: SizedBox(
width: canvasWidthPx,
height: canvasHeightPx,
Expand Down Expand Up @@ -181,8 +175,8 @@ class _PrintPageState extends State<PrintPage> {
color: Colors.black,
),
),
child: RepaintBoundary(
key: couponKey,
child: Screenshot(
controller: couponScreenshotController,
child: SizedBox(
width: canvasWidthPx,
height: canvasHeightPx,
Expand Down
14 changes: 9 additions & 5 deletions lib/printclient.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui';
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';

Future<int> printImageToLabel(
Image imageToPrint, int vendorId, int productId) async {
Uint8List imageUint8List, int vendorId, int productId) async {
var url = Uri.parse(
"http://0.0.0.0:5000/print/$vendorId/$productId?print_canvas_width_mm=70&print_canvas_height_mm=70&margin_top_px=0&margin_left_px=0&print_dpi=203");
// imageToPrint to uint8list
var img_byte_data =
await imageToPrint.toByteData(format: ImageByteFormat.png);
var image_uint8list = img_byte_data!.buffer.asUint8List();
var multipartFile = http.MultipartFile.fromBytes("image", image_uint8list,
// var imgByteData = await imageToPrint.toByteData(format: ImageByteFormat.png);
// var imageUnit8List = imgByteData!.buffer.asUint8List();
File origFile = File("./image_flutter.png")..writeAsBytesSync(imageUint8List);
var multipartFile = http.MultipartFile.fromBytes("image", imageUint8List,
filename: 'label.png', contentType: MediaType.parse('image/png'));
var request = http.MultipartRequest("POST", url);
// attach image file on multipart formadta
request.files.add(multipartFile);

var response = await request.send();
return response.statusCode;
// return 200;
}

Future<List<dynamic>> getPrinterList() async {
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.1.9"
screenshot:
dependency: "direct main"
description:
name: screenshot
sha256: "63817697a7835e6ce82add4228e15d233b74d42975c143ad8cfe07009fab866b"
url: "https://pub.dev"
source: hosted
version: "3.0.0"
shared_preferences:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies:
http: ^1.2.1
google_fonts: ^6.2.1
nm: ^0.5.0
screenshot: ^3.0.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit ab9d617

Please sign in to comment.