使用热敏打印机android打印pdf /图像文件

时间:2019-03-19 07:05:29

标签: android ethernet thermal-printer

我正在尝试使用存储在手机本地存储中的热敏打印机打印图像。但是它正在打印非常小的图像。谁能建议我如何通过热敏打印机(以太网)打印图像

代码: MainActivity.java:

public class MainActivity extends Activity {
 Socket socket = null;
 RelativeLayout relativeLayout;
  String htmlDocument;
  private WebView myWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView =(WebView)findViewById(R.id.myWebView);
        relativeLayout = (RelativeLayout) findViewById(R.id.print);
        htmlDocument = "<html><body><h2>Basic HTML Table</h2><table style=\"width:30%\"><tr><th>Firstname</th><th>Lastname</th><th>Age</th></tr><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr><tr><td>John</td><td>Doe</td><td>80</td></tr></table></body></html>";
        myWebView.loadData(htmlDocument, "text/HTML", "UTF-8");

        new Timer().schedule(new TimerTask() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void run() {
                laytooimag();
                // this code will be executed after 2 seconds
            }
        }, 5000);
    }
 public void laytooimag() {
        relativeLayout.setDrawingCacheEnabled(true);
        relativeLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        relativeLayout.layout(0, 0, relativeLayout.getMeasuredWidth(), relativeLayout.getMeasuredHeight());
        relativeLayout.buildDrawingCache();
        Bitmap bm = Bitmap.createBitmap(relativeLayout.getDrawingCache());
        relativeLayout.setDrawingCacheEnabled(false);
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/jpeg");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

        File f = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
        Log.v("Tag_file",""+f);
        try {
            f.createNewFile();
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        new EthernetPrint()
                .execute(resultdata);
    }
    private class EthernetPrint extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            try {
                resultdata = params[0];
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }

                // socket = new Socket(r.printerDetails.ipAddress, r.printerDetails.port);
                socket = new Socket("192.168.1.23", 9100);
                DataOutputStream DOS = new DataOutputStream(socket.getOutputStream());
                byte[] printformat = new byte[]{0x1B, 0x21, 0x03};
                DOS.write(printformat);
                Printerinterface bb = new Printerinterface(getApplicationContext(), "Ethernet", null, DOS);
                Log.v("Tag_d",""+"ethdoin");
                printKot_invoice(bb);
                resultdata = "Executed";
                DOS.flush();
                DOS.close();
                socket.close();
            } catch (IOException e) {
                resultdata = "";
                e.printStackTrace();
            }
            return resultdata;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {

        }

        @Override
        protected void onProgressUpdate(Void... values) {

            //printKot_invoice();
        }

        public void printKot_invoice(Printerinterface bb) {
            bb.printPhoto(R.drawable.testimg);
        }
}

Printerinterface.java:

public class Printerinterface {
 public void printPhoto(int img) {

        try {
            File image = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
            Bitmap bitmap =  decodeFile(image,100,100);
            if(bitmap!=null){
                byte[] command = Utils.decodeBitmap(bitmap);

               _outputstream().write(PrinterCommands.ESC_ALIGN_CENTER);
               printText(command);
            }else{
                Log.e("Print Photo error", "the file isn't exists");
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("PrintTools", "the file isn't exists");
        }
    }

    public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
        try {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //The new size we want to scale to000
            final int REQUIRED_WIDTH=WIDTH;
            final int REQUIRED_HIGHT=HIGHT;
            //Find the correct scale value. It should be the power of 2.
            int scale=1;
            while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
                scale*=2;

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        }
        catch (FileNotFoundException e) {}
        return null;
    }
}

如果我没有解码图像,则显示图像宽度和高度过大等错误。

0 个答案:

没有答案