API for printing from Android Applications

Quickly share your Android Code Snippets here...

API for printing from Android Applications

Postby N4Spd » Wed Aug 12, 2009 6:20 am

The "Send 2 Printer" application hooks into existing applications to
enable printing but you can also send the following data formats to
Send 2 Printer for printing from your own applications:
- HTML content
- Text
- Image
- Canvas draw commands (via serialized Picture)
- Canvas draw commands rendered to a Bitmap

For example:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     void printCanvasExample()
  3.  
  4.     {
  5.  
  6.        // create canvas to render on
  7.  
  8.        Picture picture = new Picture();
  9.  
  10.        Canvas c = picture.beginRecording( 240, 240 );
  11.  
  12.        
  13.  
  14.        // fill background with WHITE
  15.  
  16.        c.drawRGB( 0xFF, 0xFF, 0xFF );
  17.  
  18.        
  19.  
  20.        // draw text
  21.  
  22.        Paint p = new Paint();
  23.  
  24.        Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
  25.  
  26.         p.setTextSize( 18 );
  27.  
  28.         p.setTypeface( font );
  29.  
  30.         p.setAntiAlias(true);        
  31.  
  32.        Rect textBounds = new Rect();
  33.  
  34.        p.getTextBounds( HELLO_WORLD, 0, HELLO_WORLD.length(), textBounds );
  35.  
  36.        int x = (c.getWidth() - (textBounds.right-textBounds.left)) / 2;
  37.  
  38.        int y = (c.getHeight() - (textBounds.bottom-textBounds.top)) / 2;
  39.  
  40.        c.drawText( HELLO_WORLD, x, y, p );
  41.  
  42.        
  43.  
  44.        // draw icon
  45.  
  46.        Bitmap icon = BitmapFactory.decodeResource( getResources(), R.drawable.icon );
  47.  
  48.        c.drawBitmap( icon, 0, 0, null );
  49.  
  50.  
  51.  
  52.        // stop drawing
  53.  
  54.        picture.endRecording();
  55.  
  56.        
  57.  
  58.        // queue canvas for printing
  59.  
  60.        File f = PrintUtils.saveCanvasPictureToTempFile( picture );
  61.  
  62.        if( f != null )
  63.  
  64.        {
  65.  
  66.           PrintUtils.queuePictureStreamForPrinting( this, f );
  67.  
  68.        }
  69.  
  70.     }
  71.  
  72.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


The following post contains sample code to print the above formats and
include a sample "Test Print" eclipse project:

http://hit-mob.com/forums/viewtopic.php?f=13&t=66

More information about the "Send 2 Printer" application:

http://hit-mob.com/forums/viewtopic.php?f=13&t=58
N4Spd
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Aug 12, 2009 6:10 am

Top

Re: API for printing from Android Applications

Postby vinisha » Wed Jan 26, 2011 9:42 am

Thanks for sharing. :roll:


Canvas Print
vinisha
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Jan 26, 2011 9:41 am

Re: API for printing from Android Applications

Postby priya_agg » Mon Jan 31, 2011 8:37 am

i need an help in receipt printer,...
priya_agg
Freshman
Freshman
 
Posts: 3
Joined: Wed Jan 19, 2011 6:58 pm

Re: API for printing from Android Applications

Postby suhas1537 » Sun Mar 06, 2011 7:18 pm

hi i am using this code to acces a recieved sms write to a file and then send that file to a buetooth printer to print...i want use the built in bluetooth app using the action_send intent ..is my code correct?? cos its givin force close...and will bluetooth printer print any file i jus send to it??pls help,,,
Code: Select all
package com.example.sms;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.SmsMessage;
//import android.util.Log;
import android.widget.Toast;
//import android.widget.Toast;
public class sms extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SMSReceiver ss = new SMSReceiver();
        final String SMS_RECEIVED="android.provider.Telephony.SMS_RECEIVED";
        IntentFilter filter = new IntentFilter(SMS_RECEIVED);
        registerReceiver(ss, filter);
    }
/**
* The class is called when SMS is received.
*/
public class SMSReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
   Bundle bundle = intent.getExtras();
   Object messages[] = (Object[]) bundle.get("pdus");
   SmsMessage smsMessage[] = new SmsMessage[messages.length];
   for (int n = 0; n<messages.length; n++) {
   smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
   }
   try{ //       
       File root = new File(Environment.getExternalStorageDirectory(), "Notes");
           if (!root.exists()) {
               root.mkdirs();
           }
           //String sFileName = null;
         File gpxfile = new File(root,"sample.txt");
           FileWriter writer = new FileWriter(gpxfile);
           writer.append(smsMessage[0].getMessageBody());
           writer.flush();
           writer.close();
           //Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
            Toast t = Toast.makeText(context,gpxfile.getAbsolutePath(),Toast.LENGTH_LONG);
            t.show();
            Intent in = new Intent(); 
            in.setAction(Intent.ACTION_SEND); 
            in.setType("*/*"); 

            in.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(gpxfile)); 
            startActivity(intent); 
       
         
       

} catch (IOException ioe) {
        ioe.printStackTrace();
}


}


}
}
suhas1537
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Mar 06, 2011 7:06 pm

Re: API for printing from Android Applications

Postby Openxcell » Tue Apr 12, 2011 10:00 am

N4Spd wrote:The "Send 2 Printer" application hooks into existing applications to
enable printing but you can also send the following data formats to
Send 2 Printer for printing from your own applications:
- HTML content
- Text
- Image
- Canvas draw commands (via serialized Picture)
- Canvas draw commands rendered to a Bitmap

For example:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     void printCanvasExample()
  2.     {
  3.        // create canvas to render on
  4.        Picture picture = new Picture();
  5.        Canvas c = picture.beginRecording( 240, 240 );
  6.        
  7.        // fill background with WHITE
  8.        c.drawRGB( 0xFF, 0xFF, 0xFF );
  9.        
  10.        // draw text
  11.        Paint p = new Paint();
  12.        Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
  13.         p.setTextSize( 18 );
  14.         p.setTypeface( font );
  15.         p.setAntiAlias(true);        
  16.        Rect textBounds = new Rect();
  17.        p.getTextBounds( HELLO_WORLD, 0, HELLO_WORLD.length(), textBounds );
  18.        int x = (c.getWidth() - (textBounds.right-textBounds.left)) / 2;
  19.        int y = (c.getHeight() - (textBounds.bottom-textBounds.top)) / 2;
  20.        c.drawText( HELLO_WORLD, x, y, p );
  21.        
  22.        // draw icon
  23.        Bitmap icon = BitmapFactory.decodeResource( getResources(), R.drawable.icon );
  24.        c.drawBitmap( icon, 0, 0, null );
  25.  
  26.        // stop drawing
  27.        picture.endRecording();
  28.        
  29.        // queue canvas for printing
  30.        File f = PrintUtils.saveCanvasPictureToTempFile( picture );
  31.        if( f != null )
  32.        {
  33.           PrintUtils.queuePictureStreamForPrinting( this, f );
  34.        }
  35.     }
  36.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


The following post contains sample code to print the above formats and
include a sample "Test Print" eclipse project:

http://hit-mob.com/forums/viewtopic.php?f=13&t=66

More information about the "Send 2 Printer" application:

http://hit-mob.com/forums/viewtopic.php?f=13&t=58



This is the example of - Canvas draw commands rendered to a Bitmap
void printCanvasAsBitmapExample()
{
// create canvas to render on
Bitmap b = Bitmap.createBitmap( 240, 240, Bitmap.Config.RGB_565 );
Canvas c = new Canvas( b );

// fill background with WHITE
c.drawRGB( 0xFF, 0xFF, 0xFF );

// draw text
Paint p = new Paint();
Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
p.setTextSize( 18 );
p.setTypeface( font );
p.setAntiAlias(true);
Rect textBounds = new Rect();
p.getTextBounds( HELLO_WORLD, 0, HELLO_WORLD.length(), textBounds );
int x = (c.getWidth() - (textBounds.right-textBounds.left)) / 2;
int y = (c.getHeight() - (textBounds.bottom-textBounds.top)) / 2;
c.drawText( HELLO_WORLD, x, y, p );

// draw icon
Bitmap icon = BitmapFactory.decodeResource( getResources(), R.drawable.icon );
c.drawBitmap( icon, 0, 0, null );

// queue bitmap for printing
try
{
File f = PrintUtils.saveBitmapToTempFile( b, Bitmap.CompressFormat.PNG );
if( f != null )
{
PrintUtils.queueBitmapForPrinting( this, f, Bitmap.CompressFormat.PNG );
}
}
catch( Exception e )
{
Log.e( TAG, "failed to save/queue bitmap", e );
}
}
Openxcell
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Mon Mar 28, 2011 1:59 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests