การพิมพ์รูปภาพ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
การถ่ายและการแชร์รูปภาพเป็นการใช้งานยอดนิยมอย่างหนึ่งในอุปกรณ์เคลื่อนที่ หากแอปพลิเคชันของคุณ
ถ่ายภาพ แสดง หรืออนุญาตให้ผู้ใช้แชร์รูปภาพ คุณควรพิจารณาเปิดใช้การพิมพ์
รูปภาพเหล่านั้นในแอปพลิเคชัน ไลบรารีการสนับสนุนของ Android มีฟังก์ชันที่สะดวกสำหรับการเปิดใช้การพิมพ์รูปภาพโดยใช้
ใช้โค้ดเพียงเล็กน้อยและชุดตัวเลือกการจัดวางการพิมพ์ที่เรียบง่าย
บทเรียนนี้แสดงวิธีพิมพ์รูปภาพโดยใช้ชั้นเรียน PrintHelper
ไลบรารีการสนับสนุนเวอร์ชัน 4
พิมพ์รูปภาพ
ชั้นเรียน PrintHelper
ของ Android Support Library
วิธีง่ายๆ ในการพิมพ์ภาพ ชั้นเรียนมีตัวเลือกเลย์เอาต์รายการเดียว นั่นคือ setScaleMode()
ซึ่งช่วยให้คุณสามารถพิมพ์ด้วยตัวเลือกใดตัวเลือกหนึ่งต่อไปนี้
SCALE_MODE_FIT
- นี้
ตัวเลือกจะกำหนดขนาดรูปภาพเพื่อให้ทั้งรูปภาพแสดงภายในพื้นที่ที่พิมพ์ได้ของหน้า
SCALE_MODE_FILL
- นี้
ปรับขนาดรูปภาพให้เต็มพื้นที่ที่พิมพ์ได้ของหน้า กำลังเลือกตัวเลือกนี้
หมายความว่ามีบางส่วนของพื้นที่ด้านบนและด้านล่าง หรือขอบด้านซ้ายและขวาของรูปภาพ
ไม่ได้พิมพ์ ตัวเลือกนี้เป็นค่าเริ่มต้นหากไม่ได้ตั้งค่าโหมดการปรับขนาดไว้
ตัวเลือกการปรับขนาดทั้ง 2 รายการสำหรับ setScaleMode()
จะคงสัดส่วนภาพเดิมของรูปภาพไว้ ตัวอย่างโค้ดต่อไปนี้
แสดงวิธีสร้างอินสแตนซ์ของคลาส PrintHelper
โดยตั้งค่า
ตัวเลือกการปรับขนาด แล้วเริ่มกระบวนการพิมพ์ดังนี้
Kotlin
private fun doPhotoPrint() {
activity?.also { context ->
PrintHelper(context).apply {
scaleMode = PrintHelper.SCALE_MODE_FIT
}.also { printHelper ->
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.droids)
printHelper.printBitmap("droids.jpg - test print", bitmap)
}
}
}
Java
private void doPhotoPrint() {
PrintHelper photoPrinter = new PrintHelper(getActivity());
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.droids);
photoPrinter.printBitmap("droids.jpg - test print", bitmap);
}
วิธีการนี้สามารถเรียกได้ว่าเป็นการดำเนินการสำหรับรายการในเมนู โปรดทราบว่ารายการในเมนูสำหรับการดำเนินการที่
ไม่ควรวางอยู่ในเมนูรายการเพิ่มเติมเสมอไป (เช่น การพิมพ์) สำหรับข้อมูลเพิ่มเติม
โปรดดูข้อมูลเพิ่มเติมที่รูปแบบแถบการทำงาน
หลังจากเมธอด printBitmap()
คือ
เรียกใช้ คุณไม่จำเป็นต้องดำเนินการใดๆ เพิ่มเติมจากแอปพลิเคชันของคุณ อินเทอร์เฟซผู้ใช้การพิมพ์ของ Android
จะปรากฏขึ้น เพื่อให้ผู้ใช้สามารถเลือกเครื่องพิมพ์และตัวเลือกการพิมพ์ จากนั้นผู้ใช้สามารถพิมพ์
หรือยกเลิกการดำเนินการ หากผู้ใช้เลือกที่จะพิมพ์รูปภาพ ระบบจะสร้างงานพิมพ์ขึ้น
การแจ้งเตือนการพิมพ์จะปรากฏในแถบระบบ
หากต้องการใส่เนื้อหาเพิ่มเติมในงานพิมพ์ นอกเหนือจากรูปภาพ
สร้างเอกสารพิมพ์ โปรดดูข้อมูลเกี่ยวกับการสร้างเอกสารสำหรับพิมพ์ที่
การพิมพ์เอกสาร HTML หรือ
การพิมพ์เอกสารที่กำหนดเอง
ของเรา
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Printing photos\n\nTaking and sharing photos is one of the most popular uses for mobile devices. If your application\ntakes photos, displays them, or allows users to share images, you should consider enabling printing\nof those images in your application. The [Android Support Library](/tools/support-library) provides a convenient function for enabling image printing using a\nminimal amount of code and simple set of print layout options.\n\nThis lesson shows you how to print an image using the v4 support library [PrintHelper](/reference/androidx/print/PrintHelper) class.\n\nPrint an image\n--------------\n\nThe Android Support Library [PrintHelper](/reference/androidx/print/PrintHelper) class provides\na simple way to print images. The class has a single layout option, [setScaleMode()](/reference/androidx/print/PrintHelper#setScaleMode(int)),\nwhich allows you to print with one of two options:\n\n- [SCALE_MODE_FIT](/reference/androidx/print/PrintHelper#SCALE_MODE_FIT) - This option sizes the image so that the whole image is shown within the printable area of the page.\n- [SCALE_MODE_FILL](/reference/androidx/print/PrintHelper#SCALE_MODE_FILL) - This option scales the image so that it fills the entire printable area of the page. Choosing this setting means that some portion of the top and bottom, or left and right edges of the image is not printed. This option is the default value if you do not set a scale mode.\n\nBoth scaling options for [setScaleMode()](/reference/androidx/print/PrintHelper#setScaleMode(int)) keep the existing aspect ratio of the image intact. The following code example\nshows how to create an instance of the [PrintHelper](/reference/androidx/print/PrintHelper) class, set the\nscaling option, and start the printing process: \n\n### Kotlin\n\n```kotlin\nprivate fun doPhotoPrint() {\n activity?.also { context -\u003e\n PrintHelper(context).apply {\n scaleMode = PrintHelper.SCALE_MODE_FIT\n }.also { printHelper -\u003e\n val bitmap = BitmapFactory.decodeResource(resources, R.drawable.droids)\n printHelper.printBitmap(\"droids.jpg - test print\", bitmap)\n }\n }\n}\n```\n\n### Java\n\n```java\nprivate void doPhotoPrint() {\n PrintHelper photoPrinter = new PrintHelper(getActivity());\n photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);\n Bitmap bitmap = BitmapFactory.decodeResource(getResources(),\n R.drawable.droids);\n photoPrinter.printBitmap(\"droids.jpg - test print\", bitmap);\n}\n```\n\n\nThis method can be called as the action for a menu item. Note that menu items for actions that are\nnot always supported (such as printing) should be placed in the overflow menu. For more\ninformation, see the [Action Bar](/design/patterns/actionbar) design\nguide.\n\nAfter the [printBitmap()](/reference/androidx/print/PrintHelper#printBitmap(java.lang.String, android.graphics.Bitmap)) method is\ncalled, no further action from your application is required. The Android print user interface\nappears, allowing the user to select a printer and printing options. The user can then print the\nimage or cancel the action. If the user chooses to print the image, a print job is created and a\nprinting notification appears in the system bar.\n\nIf you want to include additional content in your printouts beyond just an image, you must\nconstruct a print document. For information on creating documents for printing, see the\n[Printing an HTML document](/training/printing/html-docs) or\n[Printing a custom document](/training/printing/custom-docs)\nlessons."]]