چاپ عکس
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
گرفتن و به اشتراک گذاری عکس یکی از محبوب ترین استفاده ها برای دستگاه های تلفن همراه است. اگر برنامه شما عکس می گیرد، آنها را نمایش می دهد یا به کاربران اجازه می دهد تصاویر را به اشتراک بگذارند، باید چاپ آن تصاویر را در برنامه خود فعال کنید. کتابخانه پشتیبانی Android یک عملکرد راحت برای فعال کردن چاپ تصویر با استفاده از حداقل مقدار کد و مجموعه ای ساده از گزینه های طرح بندی چاپ ارائه می دهد.
این درس به شما نشان می دهد که چگونه یک تصویر را با استفاده از کلاس PrintHelper
کتابخانه پشتیبانی v4 چاپ کنید.
یک تصویر چاپ کنید
کلاس Android Support Library PrintHelper
یک راه ساده برای چاپ تصاویر ارائه می دهد. کلاس دارای یک گزینه layout setScaleMode()
است که به شما امکان می دهد با یکی از دو گزینه چاپ کنید:
-
SCALE_MODE_FIT
- این گزینه اندازه تصویر را طوری تنظیم می کند که کل تصویر در ناحیه قابل چاپ صفحه نمایش داده شود. -
SCALE_MODE_FILL
- این گزینه تصویر را به گونه ای مقیاس می دهد که کل ناحیه قابل چاپ صفحه را پر کند. انتخاب این تنظیم به این معنی است که بخشی از لبههای بالا و پایین یا چپ و راست تصویر چاپ نمیشود. اگر حالت مقیاس را تنظیم نکنید، این گزینه مقدار پیش فرض است.
هر دو گزینه مقیاس بندی برای setScaleMode()
نسبت ابعاد موجود تصویر را دست نخورده نگه می دارند. مثال کد زیر نحوه ایجاد یک نمونه از کلاس PrintHelper
، تنظیم گزینه scaling و شروع فرآیند چاپ را نشان می دهد:
کاتلین
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)
}
}
}
جاوا
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);
}
این روش را می توان به عنوان عمل برای یک آیتم منو نامید. توجه داشته باشید که موارد منو برای اقداماتی که همیشه پشتیبانی نمی شوند (مانند چاپ) باید در منوی سرریز قرار داده شوند. برای اطلاعات بیشتر، راهنمای طراحی Action Bar را ببینید.
پس از فراخوانی متد printBitmap()
، هیچ اقدام دیگری از برنامه شما لازم نیست. رابط کاربری چاپ اندروید ظاهر می شود و به کاربر امکان می دهد چاپگر و گزینه های چاپ را انتخاب کند. سپس کاربر می تواند تصویر را چاپ کند یا عمل را لغو کند. اگر کاربر چاپ تصویر را انتخاب کند، یک کار چاپ ایجاد می شود و یک اعلان چاپ در نوار سیستم ظاهر می شود.
اگر می خواهید محتوای بیشتری را در چاپ های خود بگنجانید، فراتر از یک تصویر، باید یک سند چاپی بسازید. برای اطلاعات در مورد ایجاد اسناد برای چاپ، به درس های چاپ یک سند HTML یا چاپ یک سند سفارشی مراجعه کنید.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی."],[],[],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."]]