package com.icod.yk_printer_test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.szsicod.print.escpos.PrinterAPI;

public class PageModeActivity extends Activity {
private EditText tvX;
private EditText tvY;
private EditText tvWidth;
private EditText tvHeight;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_mode);

        tvHeight= (EditText) findViewById(R.id.et_height);
        tvX= (EditText) findViewById(R.id.et_x);
        tvY= (EditText) findViewById(R.id.et_y);
        tvWidth= (EditText) findViewById(R.id.et_width);
        findViewById(R.id.btn_cut).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PrinterAPI.getInstance().fullCut();
            }
        });
        findViewById(R.id.btn_print).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入页模式
                PrinterAPI.getInstance().pageMode();
                String barcode = "11111111";
                // try {
                int x=0;
                int y=0;
                int width=0;
                int height=0;
                try {
                    x=Integer.parseInt(tvX.getText().toString().trim());
                    y=Integer.parseInt(tvY.getText().toString().trim());
                    width=Integer.parseInt(tvWidth.getText().toString().trim());
                    height=Integer.parseInt(tvHeight.getText().toString().trim());
                }catch (Exception e){
                    return;
                }

                //退纸
                PrinterAPI.getInstance().printBackFlow(30);
                //设置打印区域大小和打印起始位置
                PrinterAPI.getInstance().pageModeArea(x, y, width, height);

                PrinterAPI.getInstance().printQRCode(barcode, 11, false);

                PrinterAPI.getInstance().setRelativePosition(20,0);
                PrinterAPI.getInstance().printQRCode(barcode, 11, false);
                //PrinterAPI.getInstance().pageRemoveAllData();
                //打印并退出页模式
                PrinterAPI.getInstance().printAndBackToStd();
                PrinterAPI.getInstance().pageSelectDirection(0);
                PrinterAPI.getInstance().setAlignMode(0);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    /*    String barcode = "11111111";
        // try {
        PrinterAPI.getInstance().setAlignMode(1);
        PrinterAPI.getInstance().pageModeArea(20, 0, 640, 500);
        PrinterAPI.getInstance().printQRCode(barcode, 6, false);
        // PrinterAPI.getInstance().printBarCode(73,barcode.length(),barcode);
        PrinterAPI.getInstance().printQRCode(barcode, 6, false);
        PrinterAPI.getInstance().pagePrint();
        PrinterAPI.getInstance().setAlignMode(0);*/
       /* } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }*/

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        PrinterAPI.getInstance().standardMode();
    }
}
