Example usage for android.graphics Bitmap getPixel

List of usage examples for android.graphics Bitmap getPixel

Introduction

In this page you can find the example usage for android.graphics Bitmap getPixel.

Prototype

@ColorInt
public int getPixel(int x, int y) 

Source Link

Document

Returns the Color at the specified location.

Usage

From source file:com.microsoft.projectoxford.face.samples.ui.Camera2BasicFragment.java

boolean checkl(Bitmap bitm, Bitmap bitn, double xx, double yy, double ww, double hh) {
    ww -= xx;/*from   w w  w.jav  a 2s.co m*/
    hh -= yy;
    xx -= ww / 2;
    yy -= hh / 2;
    ww += ww;
    hh += hh;
    int x = (int) xx;
    int y = (int) yy;
    int w = (int) ww;
    int h = (int) hh;
    double maxdif = -1000;
    double summ = 0, sumn = 0;
    for (int i = 0; i < w; i++) {
        for (int j = 0; j < h; j++) {
            int cm = bitm.getPixel(x + i, y + j);
            int cn = bitn.getPixel(x + i, y + j);
            double gm = Color.red(cm) * 0.299 + Color.green(cm) * 0.587 + Color.blue(cm) * 0.114;
            double gn = Color.red(cn) * 0.299 + Color.green(cn) * 0.587 + Color.blue(cn) * 0.114;
            maxdif = Math.max(maxdif, gn - gm);
            summ += gm;
            sumn += gn;
        }
    }
    summ /= w * h;
    sumn /= w * h;
    maxdif += summ - sumn;
    Log.i("Human", "maxdif=" + maxdif);
    return maxdif > 50;
}

From source file:com.creativeongreen.imageeffects.MainActivity.java

public static Bitmap myTest(Bitmap bmImage, int amplitude) {
    Bitmap bmTemp = Bitmap.createBitmap(bmImage.getWidth(), bmImage.getHeight(), bmImage.getConfig());
    // Bitmap bmTemp = bmImage.copy(Config.ARGB_8888, true);

    for (int i = 0; i < bmImage.getWidth(); i++) {
        for (int j = 0; j < bmImage.getHeight(); j++) {

            int sign;
            if (i <= bmImage.getWidth() / 2 || j <= bmImage.getHeight() / 2)
                sign = 1;/*from  w ww .  j a v  a2  s .  c om*/
            else
                sign = -1;
            int ki = (int) (i + amplitude * sign * Math.cos(j * 2 * Math.PI / bmImage.getHeight()));
            int kj = (int) (j + amplitude * sign * Math.cos(i * 2 * Math.PI / bmImage.getWidth()));
            // * Math.tan(i * 2 * Math.PI / bmImage.getWidth()));
            // * Math.sin(i * 2 * Math.PI / bmImage.getWidth()));

            // k = Math.max(0, k);
            // k = Math.min(bmImage.getHeight() - 1, k);

            if (ki >= 0 && ki < bmImage.getWidth() && kj >= 0 && kj < bmImage.getHeight())
                bmTemp.setPixel(i, j, bmImage.getPixel(ki, kj));
            else
                bmTemp.setPixel(i, j, 0x00ffffff);

        } // /for(j)
    } // /for(i)

    return bmTemp;
}

From source file:com.creativeongreen.imageeffects.MainActivity.java

public static Bitmap mozac(Bitmap bmImage, int level) {
    Bitmap bmTemp = Bitmap.createBitmap(bmImage.getWidth(), bmImage.getHeight(), bmImage.getConfig());

    for (int i = 0; i < bmImage.getWidth(); i += level) {
        for (int j = 0; j < bmImage.getHeight(); j += level) {
            int p = 0;
            int r = 0;
            int g = 0;
            int b = 0;

            // compute neighboring area index
            int kx_start = i - level;
            int kx_end = i + level;
            int ky_start = j - level;
            int ky_end = j + level;

            // filter out boundary index
            kx_start = Math.max(0, kx_start);
            kx_end = Math.min(bmImage.getWidth() - 1, kx_end);
            ky_start = Math.max(0, ky_start);
            ky_end = Math.min(bmImage.getHeight() - 1, ky_end);

            // summing and averaging color value within the neighboring area
            for (int ki = kx_start; ki <= kx_end; ki++) {
                for (int kj = ky_start; kj <= ky_end; kj++) {
                    p = bmImage.getPixel(ki, kj);
                    r += Color.red(p);
                    g += Color.green(p);
                    b += Color.blue(p);
                }/*from  ww  w  . j a v  a  2s.  c o m*/
            }

            int n = (kx_end - kx_start + 1) * (ky_end - ky_start + 1);
            r /= n;
            g /= n;
            b /= n;

            // copy color value to each pixel on neighboring area
            for (int kx = kx_start; kx <= kx_end; kx++) {
                for (int ky = ky_start; ky <= ky_end; ky++) {
                    bmTemp.setPixel(kx, ky, Color.argb(Color.alpha(p), r, g, b));
                }
            }

        } // /for(j)
    } // /for(i)

    return bmTemp;
}

From source file:com.creativeongreen.imageeffects.MainActivity.java

public static Bitmap bulging(Bitmap bmImage, int radius, int pointX, int pointY) {
    Bitmap bmTemp = Bitmap.createBitmap(bmImage.getWidth(), bmImage.getHeight(), bmImage.getConfig());
    // Bitmap bmTemp = bmImage.copy(Config.ARGB_8888, true);

    for (int i = 0; i < bmImage.getWidth(); i++) {
        for (int j = 0; j < bmImage.getHeight(); j++) {

            // get center point
            double cx = pointX; // bmImage.getWidth() / 2;
            double cy = pointY; // bmImage.getHeight() / 2;

            // compute distance to center point
            double r = Math.sqrt(Math.pow(i - cx, 2) + Math.pow(j - cy, 2));
            // compute angle atan2(y, x) in range (-PI..PI] n polar coordinate system
            double a = Math.atan2(j - cy, i - cx);
            // rn = r ^ k, k=1..2
            double rn = Math.pow(r, radius / 10.0) / (radius);
            // compute mapping point and then shift to center point
            int kx = (int) (rn * Math.cos(a) + cx);
            int ky = (int) (rn * Math.sin(a) + cy);

            if (kx >= 0 && kx < bmImage.getWidth() && ky >= 0 && ky < bmImage.getHeight())
                bmTemp.setPixel(i, j, bmImage.getPixel(kx, ky));
            else//from   ww  w.  j  a va2s  .c o  m
                bmTemp.setPixel(i, j, 0x00ffffff);

        } // /for(j)
    } // /for(i)

    return bmTemp;
}

From source file:com.ifoer.util.NetPOSPrinter.java

public int printPic(Bitmap bmp) {
    WifiManager wifi_service = (WifiManager) this.mContext.getSystemService("wifi");
    DhcpInfo dhcpinfo = wifi_service.getDhcpInfo();
    WifiInfo wifi_info = wifi_service.getConnectionInfo();
    if (wifi_info != null && wifi_info.getSSID() != null && !wifi_info.getSSID().startsWith("X-431PRINTER")) {
        return PRINT_NOT_CONNECT;
    }/*from w  w w .j a v a 2 s  .  c  om*/
    this.serverAddress = dhcpinfo.serverAddress;
    try {
        this.wifiSocket = new Socket(Formatter.formatIpAddress(this.serverAddress), PRINT_PORT);
        this.dos = new DataOutputStream(this.wifiSocket.getOutputStream());
        this.in = new DataInputStream(this.wifiSocket.getInputStream());
    } catch (UnknownHostException e1) {
        e1.printStackTrace();
    } catch (IOException e12) {
        e12.printStackTrace();
    }
    if (this.dos == null || this.in == null) {
        return PRINT_NOT_CONNECT;
    }
    byte[] data = new byte[3];
    data[0] = (byte) 27;
    data[ERROR_PRINT_JAM] = (byte) 51;
    try {
        this.dos.write(data, 0, data.length);
        data[0] = (byte) 0;
        data[ERROR_PRINT_JAM] = (byte) 0;
        data[ERROR_PRINT_WILL_NO_PAPER] = (byte) 0;
    } catch (IOException e122) {
        e122.printStackTrace();
    }
    byte[] escj = new byte[3];
    escj[0] = (byte) 27;
    escj[ERROR_PRINT_JAM] = (byte) 74;
    int i = 3;
    byte[] esccheck = new byte[] { (byte) 29, (byte) 114, (byte) 73 };
    byte[] escBmp = new byte[5];
    escBmp[0] = (byte) 27;
    escBmp[ERROR_PRINT_JAM] = (byte) 42;
    escBmp[ERROR_PRINT_WILL_NO_PAPER] = SmileConstants.TOKEN_LITERAL_NULL;
    escBmp[3] = (byte) (bmp.getWidth() % KEYRecord.OWNER_ZONE);
    escBmp[ERROR_PRINT_NO_PAPER] = (byte) (bmp.getWidth() / KEYRecord.OWNER_ZONE);
    for (int i2 = 0; i2 < (bmp.getHeight() / 24) + ERROR_PRINT_JAM; i2 += ERROR_PRINT_JAM) {
        try {
            this.dos.write(escBmp, 0, escBmp.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int j = 0; j < bmp.getWidth(); j += ERROR_PRINT_JAM) {
            for (int k = 0; k < 24; k += ERROR_PRINT_JAM) {
                if ((i2 * 24) + k < bmp.getHeight()) {
                    if (Color.red(bmp.getPixel(j, (i2 * 24) + k)) == 0) {
                        int i3 = k / ERROR_PRINT_ACTUATOR_FAULT;
                        data[i3] = (byte) (data[i3]
                                + ((byte) (ERROR_PRINT_HEAD_OVERHEATING >> (k % ERROR_PRINT_ACTUATOR_FAULT))));
                    }
                }
            }
            try {
                this.dos.write(data, 0, data.length);
                data[0] = (byte) 0;
                data[ERROR_PRINT_JAM] = (byte) 0;
                data[ERROR_PRINT_WILL_NO_PAPER] = (byte) 0;
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
        try {
            if (i2 % 10 == 0) {
                this.dos.write(esccheck);
                if (this.in.readByte() == null) {
                    this.dos.write(escj, 0, escj.length);
                }
            } else {
                this.dos.write(escj, 0, escj.length);
            }
        } catch (IOException e22) {
            e22.printStackTrace();
        }
    }
    i = 3;
    byte[] escf = new byte[] { (byte) 29, (byte) 122, (byte) 49 };
    i = 3;
    byte[] esck = new byte[] { (byte) 27, (byte) 74, (byte) 64 };
    try {
        this.dos.write(escf);
        this.dos.write(esck);
        this.result = this.in.readByte();
        try {
            this.dos.close();
            this.in.close();
        } catch (IOException e222) {
            e222.printStackTrace();
        }
        return this.result;
    } catch (IOException e3) {
        return PRINT_NOT_CONNECT;
    }
}

From source file:android.webkit.cts.WebViewTest.java

private boolean isPictureFilledWithColor(Picture picture, int color) {
    if (picture.getWidth() == 0 || picture.getHeight() == 0)
        return false;

    Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Config.ARGB_8888);
    picture.draw(new Canvas(bitmap));

    for (int i = 0; i < bitmap.getWidth(); i++) {
        for (int j = 0; j < bitmap.getHeight(); j++) {
            if (color != bitmap.getPixel(i, j)) {
                return false;
            }/*from w  w  w  .  j  ava2s  . c om*/
        }
    }
    return true;
}

From source file:org.kde.necessitas.ministro.ExtractStyle.java

public JSONObject getDrawable(Object drawable, String filename) {
    JSONObject json = new JSONObject();
    Bitmap bmp = null;
    if (drawable instanceof Bitmap)
        bmp = (Bitmap) drawable;//from  w w  w . j av  a2s  .co  m
    else {
        if (drawable instanceof BitmapDrawable)
            bmp = ((BitmapDrawable) drawable).getBitmap();
        else {
            if (drawable instanceof LayerDrawable) {
                return getLayerDrawable(drawable, filename);
            }
            if (drawable instanceof StateListDrawable) {
                return getStateListDrawable(drawable, filename);
            }
            if (drawable instanceof GradientDrawable) {
                return getGradientDrawable((GradientDrawable) drawable);
            }
            if (drawable instanceof ClipDrawable) {
                try {
                    json.put("type", "clipDrawable");
                    json.put("drawable",
                            getDrawable(
                                    getClipStateDrawableObject(((ClipDrawable) drawable).getConstantState()),
                                    filename));
                    Rect padding = new Rect();
                    if (((Drawable) drawable).getPadding(padding))
                        json.put("padding", getJsonRect(padding));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return json;
            }
            if (drawable instanceof ColorDrawable) {
                bmp = Bitmap.createBitmap(1, 1, Config.ARGB_8888);
                Drawable d = (Drawable) drawable;
                d.setBounds(0, 0, 1, 1);
                d.draw(new Canvas(bmp));
                Rect padding = new Rect();
                try {
                    json.put("type", "color");
                    json.put("color", bmp.getPixel(0, 0));
                    if (d.getPadding(padding))
                        json.put("padding", getJsonRect(padding));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return json;
            } else {
                Drawable d = (Drawable) drawable;
                int w = d.getIntrinsicWidth();
                int h = d.getIntrinsicHeight();
                d.setLevel(10000);
                if (w < 1 || h < 1) {
                    w = 100;
                    h = 100;
                }
                bmp = Bitmap.createBitmap(w, h, Config.ARGB_8888);
                d.setBounds(0, 0, w, h);
                d.draw(new Canvas(bmp));
                if (drawable instanceof NinePatchDrawable) {
                    NinePatchDrawable npd = (NinePatchDrawable) drawable;
                    try {
                        json.put("type", "9patch");
                        json.put("drawable", getDrawable(bmp, filename));
                        Rect padding = new Rect();
                        if (npd.getPadding(padding))
                            json.put("padding", getJsonRect(padding));
                        json.put("chunkInfo", findPatchesMarings(d));
                        return json;
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    FileOutputStream out;
    try {
        filename = m_extractPath + filename + ".png";
        out = new FileOutputStream(filename);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        json.put("type", "image");
        json.put("path", filename);
        json.put("width", bmp.getWidth());
        json.put("height", bmp.getHeight());
        MinistroActivity.nativeChmode(filename, 0644);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return json;
}

From source file:com.skytree.epubtest.BookViewActivity.java

private Drawable changeDrawableColor(Drawable drawable, int fromColor, int color) {
    Bitmap src = ((BitmapDrawable) drawable).getBitmap();
    Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
    for (int x = 0; x < bitmap.getWidth(); x++) {
        for (int y = 0; y < bitmap.getHeight(); y++) {
            if (colorMatched(bitmap.getPixel(x, y), fromColor, 10)) {
                bitmap.setPixel(x, y, color);
            }//from w w w .  j  av  a  2s  .com
        }
    }
    return new BitmapDrawable(bitmap);
}