码迷,mamicode.com
首页 > 其他好文 > 详细

andriod 蓝牙打印问题

时间:2015-04-29 15:12:13      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:android   蓝牙   gprinter   

用网上的一个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);这个方法调用里面,原有方法:

<span style="color:#333333;"> 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;

    </span><span style="color:#ff0000;">float scaleWidth = newWidth / width;
    float scaleHeight = newHeight / height;</span><span style="color:#333333;">

    Matrix matrix = new Matrix();

    matrix.postScale(scaleWidth, scaleHeight);

    Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, 
      height, matrix, true);

    return resizedBitmap;
  }</span>

这里计算的单位换算 float=int/int>>>float=int*1.0f/int就不会报错了,否则平板上会报错



andriod 蓝牙打印问题

标签:android   蓝牙   gprinter   

原文地址:http://blog.csdn.net/anddroid_lanyan/article/details/45365733

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!