andriod 藍芽列印問題

來源:互聯網
上載者:User

andriod 藍芽列印問題

用網上的一個gprinter.jar開發藍芽與收銀機的對接列印,出現如下錯誤:x + width must be <= bitmap.width()

zs這一系列的錯誤都源自於矩陣的檢測:

 

 /**     * Shared code to check for illegal arguments passed to getPixels()     * or setPixels()     *     * @param x left edge of the area of pixels to access     * @param y top edge of the area of pixels to access     * @param width width of the area of pixels to access     * @param height height of the area of pixels to access     * @param offset offset into pixels[] array     * @param stride number of elements in pixels[] between each logical row     * @param pixels array to hold the area of pixels being accessed    */    private void checkPixelsAccess(int x, int y, int width, int height,                                   int offset, int stride, int pixels[]) {        checkXYSign(x, y);        if (width < 0) {            throw new IllegalArgumentException("width must be >= 0");        }        if (height < 0) {            throw new IllegalArgumentException("height must be >= 0");        }        if (x + width > getWidth()) {            throw new IllegalArgumentException(                    "x + width must be <= bitmap.width()");        }        if (y + height > getHeight()) {            throw new IllegalArgumentException(                    "y + height must be <= bitmap.height()");        }        if (Math.abs(stride) < width) {            throw new IllegalArgumentException("abs(stride) must be >= width");        }        int lastScanline = offset + (height - 1) * stride;        int length = pixels.length;        if (offset < 0 || (offset + width > length)                || lastScanline < 0                || (lastScanline + width > length)) {            throw new ArrayIndexOutOfBoundsException();        }    }

研究了哈原始碼發現問題出在resizeImage(mBitmap, width, height);這個方法調用裡面,原有方法:

 

 

 public static Bitmap resizeImage(Bitmap bitmap, int w, int h)  {    Bitmap BitmapOrg = bitmap;    int width = BitmapOrg.getWidth();    int height = BitmapOrg.getHeight();    int newWidth = w;    int newHeight = h;    float scaleWidth = newWidth / width;    float scaleHeight = newHeight / height;    Matrix matrix = new Matrix();    matrix.postScale(scaleWidth, scaleHeight);    Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,       height, matrix, true);    return resizedBitmap;  }

這裡計算的單位換算 float=int/int>>>float=int*1.0f/int就不會報錯了,否則平板上會報錯

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.