Example usage for android.graphics Bitmap setPixel

List of usage examples for android.graphics Bitmap setPixel

Introduction

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

Prototype

public void setPixel(int x, int y, @ColorInt int color) 

Source Link

Document

Write the specified Color into the bitmap (assuming it is mutable) at the x,y coordinate.

Usage

From source file:org.fedorahosted.freeotp.main.ScanDialogFragment.java

@Override
public void process(Frame frame) {
    byte[] i = frame.getImage();
    Resolution r = frame.getSize();//w  w w .j  ava 2 s.co  m

    LuminanceSource ls = new PlanarYUVLuminanceSource(i, r.width, r.height, 0, 0, r.width, r.height, false);

    try {
        BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(ls));
        final String uri = new QRCodeReader().decode(bb).getText();

        int size = mImage.getWidth();
        if (size > mImage.getHeight())
            size = mImage.getHeight();

        BitMatrix bm = new QRCodeWriter().encode(uri, BarcodeFormat.QR_CODE, size, size);
        mFotoapparat.stop();

        final Bitmap b = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        for (int x = 0; x < size; x++) {
            for (int y = 0; y < size; y++) {
                b.setPixel(x, y, bm.get(x, y) ? Color.BLACK : Color.WHITE);
            }
        }

        Vibrator v = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            v.vibrate(VibrationEffect.createOneShot(250, VibrationEffect.DEFAULT_AMPLITUDE));
        } else {
            v.vibrate(250);
        }

        mImage.post(new Runnable() {
            @Override
            public void run() {
                mProgress.setVisibility(View.INVISIBLE);
                mCamera.animate().setInterpolator(new DecelerateInterpolator()).setDuration(2000).alpha(0.0f)
                        .start();

                mImage.setImageBitmap(b);
                mImage.animate().setInterpolator(new DecelerateInterpolator()).setDuration(2000).alpha(1.0f)
                        .withEndAction(new Runnable() {
                            @Override
                            public void run() {
                                mImage.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        Activity a = (Activity) getActivity();
                                        a.addToken(Uri.parse(uri), true);
                                    }
                                });
                                dismiss();
                            }
                        }).start();
            }
        });
    } catch (NotFoundException | ChecksumException | FormatException | WriterException e) {
        e.printStackTrace();
    }
}

From source file:org.thezero.qrfi.SuperAwesomeCardFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View rootView;
    if (position == 0) {
        rootView = inflater.inflate(R.layout.fragment_generate, container, false);
        final CardView cardQr = (CardView) rootView.findViewById(R.id.card_qr);
        cardQr.setVisibility(View.GONE);

        String[] items = new String[] { "Open", "WEP", "WPA/WPA2" };
        final EditText ssid = (EditText) rootView.findViewById(R.id.edit_ssid);
        final EditText pass = (EditText) rootView.findViewById(R.id.edit_pass);
        final Spinner spinner = (Spinner) rootView.findViewById(R.id.wifi_type);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(c, android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);//from  www  .j av  a  2  s  .  co m
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (parent.getSelectedItem().toString().equals("Open")) {
                    rootView.findViewById(R.id.second).setVisibility(View.GONE);
                } else {
                    rootView.findViewById(R.id.second).setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        final boolean[] cardHide = { true };
        Button delete = (Button) rootView.findViewById(R.id.delete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cardHide[0] = true;
                cardQr.animate().translationY(0).alpha(0.0f).setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        if (cardHide[0])
                            cardQr.setVisibility(View.GONE);
                    }
                });
                ssid.setText("");
                pass.setText("");
                spinner.setSelection(0);
            }
        });

        Button gen = (Button) rootView.findViewById(R.id.generate);
        gen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // WIFI:S:Pistol;T:WEP;P:pistolawifi2;;
                if (pass.getText().length() < 8 && !spinner.getSelectedItem().toString().equals("Open")) {
                    Toast.makeText(c, c.getString(R.string.error_pass), Toast.LENGTH_LONG).show();
                    return;
                }
                String ap = "WIFI:S:" + ssid.getText() + ";T:" + spinner.getSelectedItem().toString() + ";P:"
                        + pass.getText() + ";;";
                Log.d(TAG, ap);
                QRCodeWriter writer = new QRCodeWriter();
                try {
                    BitMatrix matrix = writer.encode(ap, BarcodeFormat.QR_CODE, 700, 700, null);
                    int height = matrix.getHeight();
                    int width = matrix.getWidth();
                    final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
                    for (int x = 0; x < width; x++) {
                        for (int y = 0; y < height; y++) {
                            bmp.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.WHITE);
                        }
                    }
                    ImageView qr_image = (ImageView) rootView.findViewById(R.id.qrimage);
                    qr_image.setImageBitmap(bmp);
                    cardHide[0] = false;
                    cardQr.setVisibility(View.VISIBLE);
                    cardQr.setAlpha(1.0f);
                    cardQr.animate().translationY(10).alpha(1.0f);

                    Button dialogButton = (Button) rootView.findViewById(R.id.ok);
                    dialogButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            File sdcard = Environment.getExternalStorageDirectory();
                            FileOutputStream out = null;
                            File check = new File(sdcard, "/wifipv/");
                            if (!(check.exists())) {
                                boolean resu = check.mkdir();
                                if (!resu) {
                                    return;
                                }
                            }

                            try {
                                File myFile = new File(sdcard, "/wifipv/" + ssid.getText() + ".png");
                                out = new FileOutputStream(myFile);
                                boolean success = bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
                                if (success) {
                                    MediaScannerConnection.scanFile(c,
                                            new String[] { myFile.getAbsolutePath() }, null,
                                            new MediaScannerConnection.OnScanCompletedListener() {
                                                @Override
                                                public void onScanCompleted(String path, Uri uri) {
                                                    Log.d(TAG, "file " + path + " was scanned seccessfully: "
                                                            + uri);
                                                    Intent shareIntent = new Intent();
                                                    shareIntent.setAction(Intent.ACTION_SEND);
                                                    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                                                    shareIntent.setType("image/png");
                                                    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                                    Intent new_intent = Intent.createChooser(shareIntent,
                                                            c.getText(R.string.send_to));
                                                    new_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                                    c.startActivity(new_intent);

                                                }
                                            });
                                } else {
                                    Toast.makeText(c, c.getString(R.string.error_share), Toast.LENGTH_LONG)
                                            .show();
                                }
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } finally {
                                if (out != null)
                                    try {
                                        out.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                            }
                        }
                    });
                } catch (WriterException e) {
                    e.printStackTrace();
                }
            }
        });
    } else {
        rootView = inflater.inflate(R.layout.fragment_scan, container, false);
        Button scan = (Button) rootView.findViewById(R.id.scan);
        scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(c, ScanActivity.class);
                myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                c.startActivity(myIntent);
            }
        });
        Button gallery = (Button) rootView.findViewById(R.id.gallery);
        gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectFromGallery();
            }
        });
        /*final boolean[] tutorialHide = {false};
        final CardView tutorial = (CardView)rootView.findViewById(R.id.card_tutorial);
        tutorial.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                
            if(tutorialHide[0]){
                tutorialHide[0]=false;
                Animation anidelta = new ResizeAnimation(tutorial, tutorial.getHeight());
                anidelta.setDuration(500);
                anidelta.setFillAfter(true);
                tutorial.startAnimation(anidelta);
            }else{
                tutorialHide[0]=true;
                Animation anidelta = new ResizeAnimation(tutorial, 120);
                anidelta.setDuration(500);
                anidelta.setFillAfter(true);
                tutorial.startAnimation(anidelta);
            }
        }
        });*/
    }
    ViewCompat.setElevation(rootView, 50);
    return rootView;
}

From source file:com.android.profilerapp.memory.MemoryFragment.java

private void allocateBigBitmapArray(View view, boolean init) {
    Bitmap map = Bitmap.createBitmap(1024, 1024, Bitmap.Config.ALPHA_8);
    if (init) {/*from   ww  w  .j a va 2  s  . c o  m*/
        for (int i = 0; i < map.getHeight(); i++) {
            for (int j = 0; j < map.getWidth(); j++) {
                map.setPixel(j, i, 0);
            }
        }
    }
    mBitmaps.add(map);
}

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

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

    for (int i = 0; i < bmImage.getWidth(); i++) {
        for (int j = 0; j < bmImage.getHeight(); j++) {
            int p = bmImage.getPixel(i, j);
            bmTemp.setPixel(i, j, (p & 0xff000000) | (~p & 0x00ffffff));
        }/*from w w w  .  j  a v  a 2s  . co  m*/
    }

    return bmTemp;
}

From source file:com.hacktx.android.activities.CheckInActivity.java

private void loadQrCode(final String email) {
    new Thread(new Runnable() {
        public void run() {
            QRCodeWriter writer = new QRCodeWriter();
            final Bitmap code;
            try {
                if (!isCodeSaved()) {
                    BitMatrix bitMatrix = writer.encode(email, BarcodeFormat.QR_CODE, 512, 512);
                    int width = bitMatrix.getWidth();
                    int height = bitMatrix.getHeight();
                    code = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
                    for (int x = 0; x < width; x++) {
                        for (int y = 0; y < height; y++) {
                            code.setPixel(x, y,
                                    bitMatrix.get(x, y) ? Color.BLACK
                                            : ContextCompat.getColor(CheckInActivity.this,
                                                    R.color.cardview_light_background));
                        }//from ww  w  . j a v a  2s . c o  m
                    }

                    saveCodeToInternalStorage(code);
                } else {
                    code = loadCodeFromInternalStorage();
                }

                CheckInActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ((ImageView) findViewById(R.id.codeCardCode)).setImageBitmap(code);
                        findViewById(R.id.codeCardProgressBar).setVisibility(View.GONE);
                        findViewById(R.id.codeCardCode).setVisibility(View.VISIBLE);
                        findViewById(R.id.codeCardButtons).setVisibility(View.VISIBLE);
                    }
                });
            } catch (WriterException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

From source file:java_lang_programming.com.android_media_demo.article80.ImageFragment.java

/**
 * Bitmap??/*www  . ja  va  2  s .co  m*/
 * ??
 * grayscale using Bitmap
 * SimpleMeanMethod
 */
private void grayScaleSimpleMeanMethod() {
    long start = System.currentTimeMillis();
    Bitmap mutableBitmap = getMutableBitmap();

    int width = mutableBitmap.getWidth();
    int height = mutableBitmap.getHeight();
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {

            // Returns the Color at the specified location.
            int pixel = mutableBitmap.getPixel(i, j);

            int red = Color.red(pixel);
            int green = Color.green(pixel);
            int blue = Color.blue(pixel);

            int average = (red + green + blue) / 3;
            int gray_rgb = Color.rgb(average, average, average);

            mutableBitmap.setPixel(i, j, gray_rgb);
        }
    }
    imageView.setImageBitmap(mutableBitmap);
    long end = System.currentTimeMillis();
    Log.d(TAG, ((end - start) + "ms"));
}

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

public Bitmap bitmapFanzhuan(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    bitmap.getPixels(new int[(width * height)], 0, width, 0, 0, width, height);
    for (int i = 0; i < height; i += ERROR_PRINT_JAM) {
        for (int j = 0; j < width; j += ERROR_PRINT_JAM) {
            int grey = bitmap.getPixel(j, i);
            if (((((16711680 & grey) >> 16)
                    + ((MotionEventCompat.ACTION_POINTER_INDEX_MASK & grey) >> ERROR_PRINT_ACTUATOR_FAULT))
                    + (grey & KEYRecord.PROTOCOL_ANY)) / 3 < 100) {
                bitmap.setPixel(j, i, DefaultRenderer.BACKGROUND_COLOR);
            } else {
                bitmap.setPixel(j, i, -1);
            }//from w  w  w.  j av  a 2  s  .c om
        }
    }
    return bitmap;
}

From source file:java_lang_programming.com.android_media_demo.article80.ImageFragment.java

/**
 * Bitmap??// w w  w. j  a  v  a  2s. c  o  m
 * NTSC ???
 * grayscale using Bitmap
 * NTSC Coef. method
 */
private void grayScaleNTSCCoefMethod() {
    long start = System.currentTimeMillis();
    Bitmap mutableBitmap = getMutableBitmap();

    int width = mutableBitmap.getWidth();
    int height = mutableBitmap.getHeight();
    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {

            // Returns the Color at the specified location.
            int pixel = mutableBitmap.getPixel(i, j);

            int red = Color.red(pixel);
            int green = Color.green(pixel);
            int blue = Color.blue(pixel);

            // RGB ??
            double gray_scale_value = 0.2989 * red + 0.5870 * green + 0.1140 * blue;
            int gray_scale = (int) gray_scale_value;

            int gray_rgb = Color.rgb(gray_scale, gray_scale, gray_scale);

            mutableBitmap.setPixel(i, j, gray_rgb);
        }
    }
    imageView.setImageBitmap(mutableBitmap);
    long end = System.currentTimeMillis();
    Log.d(TAG, ((end - start) + "ms"));
}

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

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

    for (int i = 0; i < bmImage.getWidth(); i++) {
        for (int j = 0; j < bmImage.getHeight(); j++) {
            int p = bmImage.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);

            r += brightness;//from  ww  w  . j  a v  a2  s .com
            g += brightness;
            b += brightness;
            alpha += brightness;
            r = r < 0 ? 0 : (r > 255 ? 255 : r);
            g = g < 0 ? 0 : (g > 255 ? 255 : g);
            b = b < 0 ? 0 : (b > 255 ? 255 : b);
            alpha = alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha);

            bmTemp.setPixel(i, j, Color.argb(alpha, r, g, b));
        }
    }

    return bmTemp;
}

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

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

    // The W3C Algorithm
    double intensityRed = 0.299;
    double intensityGreen = 0.587;
    double intensityBlue = 0.114;

    for (int i = 0; i < bmImage.getWidth(); i++) {
        for (int j = 0; j < bmImage.getHeight(); j++) {
            int p = bmImage.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);

            /*/*from www  . j  a  v  a 2s.  com*/
             * r = g = b = (int) (r * intensityRed + g * intensityGreen + b intensityBlue);
             * 
             * bmTemp.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
             */
            // int rgb = (r * 77 + g * 151 + b * 28) >> 8; // NTSC luma
            int rgb = (int) Math.sqrt(r * r * 0.241 + g * g * 0.691 + b * b * 0.068); // HSP, where the P stands for Perceived brightness
            bmTemp.setPixel(i, j, (p & 0xff000000) | (rgb << 16) | (rgb << 8) | rgb);
        }
    }

    return bmTemp;
}