2 * Copyright (C) 2013 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.gallery3d.util;
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Matrix;
22 import android.graphics.RectF;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.os.CancellationSignal;
26 import android.os.ParcelFileDescriptor;
27 import android.print.PageRange;
28 import android.print.PrintAttributes;
29 import android.print.PrintDocumentAdapter;
30 import android.print.PrintDocumentInfo;
31 import android.print.PrintManager;
32 import android.print.pdf.PdfDocument.Page;
33 import android.print.pdf.PrintedPdfDocument;
35 import com.android.gallery3d.filtershow.cache.ImageLoader;
37 import java.io.FileOutputStream;
38 import java.io.IOException;
40 public class PrintJob {
41 private final static int MAX_PRINT_SIZE = 2048;
43 public static void printBitmap(final Context context, final String jobName,
44 final Bitmap bitmap) {
48 PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
49 printManager.print(jobName,
50 new PrintDocumentAdapter() {
51 private PrintAttributes mAttributes;
54 public void onLayout(PrintAttributes oldPrintAttributes,
55 PrintAttributes newPrintAttributes,
56 CancellationSignal cancellationSignal,
57 LayoutResultCallback layoutResultCallback,
60 mAttributes = newPrintAttributes;
62 PrintDocumentInfo info = new PrintDocumentInfo
63 .Builder(jobName, newPrintAttributes)
64 .setContentType(PrintDocumentInfo.CONTENT_TYPE_PHOTO)
65 .setColorMode(PrintAttributes.COLOR_MODE_COLOR)
69 layoutResultCallback.onLayoutFinished(info, false);
73 public void onWrite(PageRange[] pageRanges, ParcelFileDescriptor fileDescriptor,
74 CancellationSignal cancellationSignal,
75 WriteResultCallback writeResultCallback) {
77 PrintedPdfDocument pdfDocument = PrintedPdfDocument.open(context,
79 Page page = pdfDocument.startPage(1);
81 RectF content = new RectF(page.getInfo().getContentSize());
82 Matrix matrix = new Matrix();
84 // Handle orientation.
85 if (mAttributes.getOrientation()
86 == PrintAttributes.ORIENTATION_LANDSCAPE) {
87 // Compute and apply scale based on fitting mode.
89 switch (mAttributes.getFittingMode()) {
90 case PrintAttributes.FITTING_MODE_SCALE_TO_FILL: {
91 scale = Math.max(content.width() / bitmap.getHeight(),
92 content.height() / bitmap.getWidth());
95 case PrintAttributes.FITTING_MODE_SCALE_TO_FIT: {
96 scale = Math.min(content.width() / bitmap.getHeight(),
97 content.height() / bitmap.getWidth());
104 matrix.postScale(scale, scale);
106 // Apply the rotation.
107 matrix.postRotate(90);
108 matrix.postTranslate(bitmap.getHeight() * scale, 0);
110 // Center the content.
111 final float translateX = (content.width()
112 - bitmap.getHeight() * scale) / 2;
113 final float translateY = (content.height()
114 - bitmap.getWidth() * scale) / 2;
115 matrix.postTranslate(translateX, translateY);
117 // Compute and apply scale based on fitting mode.
119 switch (mAttributes.getFittingMode()) {
120 case PrintAttributes.FITTING_MODE_SCALE_TO_FILL: {
121 scale = Math.max(content.width() / bitmap.getWidth(),
122 content.height() / bitmap.getHeight());
125 case PrintAttributes.FITTING_MODE_SCALE_TO_FIT: {
126 scale = Math.min(content.width() / bitmap.getWidth(),
127 content.height() / bitmap.getHeight());
130 matrix.postScale(scale, scale);
132 // Center the content.
133 final float translateX = (content.width()
134 - bitmap.getWidth() * scale) / 2;
135 final float translateY = (content.height()
136 - bitmap.getHeight() * scale) / 2;
137 matrix.postTranslate(translateX, translateY);
141 page.getCanvas().drawBitmap(bitmap, matrix, null);
143 // Write the document.
144 pdfDocument.finishPage(page);
145 pdfDocument.writeTo(new FileOutputStream(
146 fileDescriptor.getFileDescriptor()));
150 writeResultCallback.onWriteFinished(
151 new PageRange[] { PageRange.ALL_PAGES });
153 if (fileDescriptor != null) {
155 fileDescriptor.close();
156 } catch (IOException ioe) {
160 writeResultCallback.onWriteFailed(null);
163 }, new PrintAttributes.Builder().create());
166 public static void printBitmapAtUri(Context context, String imagePrint, Uri uri) {
167 // TODO: load full size images. For now, it's better to constrain ourselves.
168 Bitmap bitmap = ImageLoader.loadConstrainedBitmap(uri, context, MAX_PRINT_SIZE, null, false);
169 printBitmap(context, imagePrint, bitmap);