Print JPG file from Processing (printer)

Hi :slight_smile:
I try to print with usb printer from processing.
I think this project’s process is simple. if i tap enter key, image file is printed. printing is fine.
but I have problem, printer print image like this photo.

it is too small… how can i fix it?

I use peripage mini printer(via USB) and window 10.

import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import java.io.*;

void setup() {
  size(400, 400);
  println("Press Enter to print the image.");
}

void draw() {
}

void keyPressed() {
  if (keyCode == ENTER) {
    printPDF("C:/Users/user/Documents/Processing/receipt/receipt_1.jpg", "PeriPage A6+"); // 여기에 PDF 파일 경로와 프린터 이름을 넣으세요
  }
}

void printPDF(String filePath, String printerName) {
  PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
  PrintService printService = null;

  for (int i = 0; i < printServices.length; i++) {
    if (printServices[i].getName().equals(printerName)) {
      printService = printServices[i];
      break;
    }
  }

  if (printService != null) {
    FileInputStream fis = null;
    try {
      fis = new FileInputStream(filePath);
      DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
      Doc pdfDoc = new SimpleDoc(fis, flavor, null);
      DocPrintJob printJob = printService.createPrintJob();
      printJob.print(pdfDoc, null);
      println("Printing image...");
    } catch (FileNotFoundException | PrintException e) {
      e.printStackTrace();
    } finally {
      if (fis != null) {
        try {
          fis.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  } else {
    println("Printer not found: " + printerName);
  }
}

Thank you

Not sure it will make a difference, but have you tried DocFlavor.INPUT_STREAM.JPEG? Reference is here: https://docs.oracle.com/javase%2F7%2Fdocs%2Fapi%2F%2F/javax/print/DocFlavor.INPUT_STREAM.html

A6 paper is a lot smaller than standard 8.5" x 11" typing paper and it may be scaling your image down in size to fit the paper. I tried to run your code and just now realized that you are using a special label printer, which I don’t have, and therefore will be unable to reproduce your problem. So it doesn’t look like I’m going to be much help. Perhaps someone else has this printer and could test your code and image.

is it right? I post the image I want to print…
sorry English is not my first language

this picture is the image that I want to print.
I think your guess is right. but there are no ways(like code things…) to adjust printing image size?

there are no ways(like code things…) to adjust printing image size?

I agree that is a valid point in trying to solve your problem. So far I’ve had no luck with Google searches.

  1. Do you see a print dialog when you use DocFlavor? On a Mac it allows you to manually set the scale.

  2. There is a Printer Resolution Class referenced here: https://javadoc.scijava.org/Java6/javax/print/attribute/standard/PrinterResolution.html

I have no experience using it.

Addendum:
Your receipt image measures 301 x 605 which is fairly small for an image. When printed on 8.5" x 11" paper it measures approximately 2" x 4" (width by length) which is about what I would expect for a receipt. Have you tried printing it out on another printer? Is there a setup dialog for your PeriPage label printer?

Why not just export PNG and print that using Preview or Krita or something.

Windows also has a tendency to have issues.

Try it on Mac or Linux, try a different file type, or try a different image.

Processing may not be the best option for this; Java may work better.

This may be helpful:

Because It has that the driver of this printer on only window version …
and I’m just a design student who learn about interactive art… i’m not developer. So I’m used to proseccing rather than java. also I have never use java before… :cry:

I already tried to use another jpg file and png file, it doesn’t work

But i will try what you wrote

really thank you !

My printer have setup dialog, but it has only one option width 2" paper. That’s why I set the size of image small.
Than if I try to adjust the size of image to fit in 8.5" 11" paper, could it be work well?..

You’ll have to try it. I don’t have the printer hardware to test what you’re doing.

Linux often works without a driver.

Mac drivers are small because most of the dependencies (like CUPS) are already installed.

Update your system and reinstall the driver if needed.