0

Problem:

Using an Android device, without internet / network connection, I have to print images to a bluetooth enabled printer.

Be aware that this is the business case — I cannot use Google Cloud Print, nor can I use PrinterShare or anything else like that. The data generated by the app is medical in nature and must be kept confidential and within the app.

What I’ve accomplished:

Generating PCL data (text only, no images) to send to a bluetooth connection that the printer can process and output.

What I’ve tried:

Generating PCL data (image) using the Android ImageMagick port (found here: https://github.com/paulasiimwe/Android-ImageMagick) and sending the generated PCL data to the bluetooth connection.

Result:

Gibberish, but gibberish that appears to be the right dimensions of the image I am trying to print.

==============

My hypothesis is that there’s something slightly off in my implementation of the conversion, but I’m not sure what it is.

Sample Code:

NOTE: this is proof of concept work and captures an image from the Assets dir, makes it available, and then converts it.

// Attempt to get image file from Assets folder
AssetManager assetManager = context.getAssets();
InputStream istr = assetManager.open("test.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(istr);

// Access External Storage Directory and save file
String root = Environment.getExternalStorageDirectory().toString();
File tempDirectory = new File(root + "/temp");
tempDirectory.mkdirs();

String fileName = "test.jpg";
File file = new File (tempDirectory, fileName);

try {
    FileOutputStream out = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
    out.flush();
    out.close();
} catch (Exception exception) {
    // Exception found
}

// Capture file of recently saved image, convert to PCL
try{
    ImageInfo originalInfo = new ImageInfo(Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp/test.jpg");
    MagickImage mImage = new MagickImage(originalInfo);

    String newInfoPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp/test.pcl";
    mImage.setFileName(newInfoPath);
    mImage.setImageFormat("pcl");

    ImageInfo newInfo = new ImageInfo(newInfoPath);
    mImage.writeImage(newInfo);
} catch (MagickException exception) {
    // Exception found
}

I then retrieve the PCL file and send it to the bluetooth connection (using another method that works using standard PCL files), but as mentioned the output is gibberish.

I feel like I’m missing something simple, but I’m just not seeing it.

Also, I am seeing the following in LogCat that may / may not be related, but I haven't been able to find any information about resolving this nor can I determine if it's anything more than a warning.

V/Magick: ThrowException
V/Magick: severity: 395
V/Magick: reason: UnableToOpenConfigureFile `policy.xml' @ warning/configure.c/GetConfigureOptions/589
V/Magick: Attempting to read from file /storage/emulated/0/temp/test.jpg
V/Magick: ThrowException
V/Magick: severity: 395
V/Magick: reason: UnableToOpenConfigureFile `magic.xml' @ warning/configure.c/GetConfigureOptions/589
V/Magick: OpenPixelCache()
V/Magick: - cache_info->columns: 144
V/Magick: - cache_info->rows: 144
V/Magick: - cache_info->offset: 0
V/Magick: - cache_info->length: 165888
V/Magick: - length: 207360
V/Magick: - create memory pixel cache
V/Magick: ReadImage completed

Does anyone have any experience with this or can anyone point me in the right direction?

  • Try using ImageMagick just at the command-line to create some simple test images... `convert -size 144x144! xc:black black.jpg`, `convert -size 144x144! gradient:white-black gradient.jpg` and see what happens when you print them. – Mark Setchell Jun 08 '16 at 10:17
  • @MarkSetchell do you mean the regular ImageMagick library or do you mean in the command line in Android Studio or something along those lines? – user3059309 Jun 08 '16 at 15:41
  • It doesn't really matter what you use - the idea was to create a couple of really simple image files and you may be able to deduce what's wrong with the code - the colour could come out wrong, the image could be skewed, and that may give you an insight - start simple, build up slowly! – Mark Setchell Jun 08 '16 at 15:42
  • I discovered something interesting @MarkSetchell -- using a standard conversion call with ImageMagick "convert test.jpg -monochrome test.pcl" work as expected ("convert test.jpg test.pcl" did not, my assumption now is that my printer supports PCL5 and not PCL6), but trying to specify monochrome PCL within the Android ImageMagick port isn't working as expected – user3059309 Jun 08 '16 at 17:32
  • I am not familiar with the Android port, but try running a `threshold` operation at 50% - that should also make a monochrome. – Mark Setchell Jun 08 '16 at 18:34
  • thanks @MarkSetchell -- I'll look into that and report back what I find – user3059309 Jun 10 '16 at 12:32
  • for future reference: the using threshold didn't work with the Android port of ImageMagick and the assumption is that the port is faulty – user3059309 Jun 16 '16 at 23:31

0 Answers0