Example usage for android.graphics Bitmap copy

List of usage examples for android.graphics Bitmap copy

Introduction

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

Prototype

public Bitmap copy(Config config, boolean isMutable) 

Source Link

Document

Tries to make a new bitmap based on the dimensions of this bitmap, setting the new bitmap's config to the one specified, and then copying this bitmap's pixels into the new bitmap.

Usage

From source file:net.evendanan.android.hagarfingerpainting.HagarFingerpaintingActivity.java

private File takeScreenshot(boolean showToast) {
    View v = getWindow().getDecorView();
    final int originalAdsVisibility = mAdView.getVisibility();
    mAdView.setVisibility(View.INVISIBLE);
    mSettingsIcons.setVisibility(View.INVISIBLE);

    v.setDrawingCacheEnabled(true);//from   w w  w.j ava2 s  . c  o m
    Bitmap cachedBitmap = v.getDrawingCache();
    Bitmap copyBitmap = cachedBitmap.copy(Bitmap.Config.RGB_565, true);
    FileOutputStream output = null;
    File file = null;
    try {
        File path = Places.getScreenshotFolder();
        Calendar cal = Calendar.getInstance();

        file = new File(path,
                getPainterName() + "_" + cal.get(Calendar.YEAR) + "_" + (1 + cal.get(Calendar.MONTH)) + "_"
                        + cal.get(Calendar.DAY_OF_MONTH) + "_" + cal.get(Calendar.HOUR_OF_DAY) + "_"
                        + cal.get(Calendar.MINUTE) + "_" + cal.get(Calendar.SECOND) + ".png");
        output = new FileOutputStream(file);
        copyBitmap.compress(CompressFormat.PNG, 100, output);
    } catch (FileNotFoundException e) {
        file = null;
        e.printStackTrace();
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        mAdView.setVisibility(originalAdsVisibility);
        mSettingsIcons.setVisibility(View.VISIBLE);
    }

    if (file != null) {
        if (showToast)
            Toast.makeText(getApplicationContext(), "Save fingerpainting to: " + file.getAbsolutePath(),
                    Toast.LENGTH_LONG).show();
        //sending a broadcast to the media scanner so it will scan the new screenshot.
        Intent requestScan = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        requestScan.setData(Uri.fromFile(file));
        sendBroadcast(requestScan);

        return file;
    } else {
        return null;
    }
}

From source file:com.tweetlanes.android.model.AccountDescriptor.java

private void initCommon(ArrayList<String> displayedLanes) {

    mShouldRefreshLists = true;/*  w w  w. j av a2 s . co  m*/

    mLaneDefinitions = new ArrayList<LaneDescriptor>();
    if (mLists == null) {
        mLists = new ArrayList<List>();
    }
    configureLaneDefinitions(displayedLanes);

    if (Constant.ENABLE_PROFILE_IMAGES) {

        FetchBitmapCallback callback = new FetchBitmapCallback() {

            @Override
            public void finished(boolean successful, Bitmap bitmap) {
                if (successful == true && bitmap != null) {
                    mProfileImage = bitmap.copy(bitmap.getConfig(), false);
                }
            }

        };
        URLFetch.fetchBitmap(
                TwitterManager.get().getProfileImageUrl(mScreenName, TwitterManager.ProfileImageSize.BIGGER),
                callback);
    }
}

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

public static Bitmap colorDodgeBlend(Bitmap source, Bitmap layer) {
    Bitmap base = source.copy(Config.ARGB_8888, true);
    Bitmap blend = layer.copy(Config.ARGB_8888, false);

    IntBuffer buffBase = IntBuffer.allocate(base.getWidth() * base.getHeight());
    base.copyPixelsToBuffer(buffBase);//from  ww  w.j  a  va  2s  . c om
    buffBase.rewind();

    IntBuffer buffBlend = IntBuffer.allocate(blend.getWidth() * blend.getHeight());
    blend.copyPixelsToBuffer(buffBlend);
    buffBlend.rewind();

    IntBuffer buffOut = IntBuffer.allocate(base.getWidth() * base.getHeight());
    buffOut.rewind();

    while (buffOut.position() < buffOut.limit()) {
        int filterInt = buffBlend.get();
        int srcInt = buffBase.get();

        int redValueFilter = Color.red(filterInt);
        int greenValueFilter = Color.green(filterInt);
        int blueValueFilter = Color.blue(filterInt);

        int redValueSrc = Color.red(srcInt);
        int greenValueSrc = Color.green(srcInt);
        int blueValueSrc = Color.blue(srcInt);

        int redValueFinal = colordodge(redValueFilter, redValueSrc);
        int greenValueFinal = colordodge(greenValueFilter, greenValueSrc);
        int blueValueFinal = colordodge(blueValueFilter, blueValueSrc);

        int pixel = Color.argb(255, redValueFinal, greenValueFinal, blueValueFinal);

        /*
         * float[] hsv = new float[3]; Color.colorToHSV(pixel, hsv); hsv[1] = 0.0f; float top =
         * VALUE_TOP; // Setting this as 0.95f gave the best result so far if (hsv[2] <= top) {
         * hsv[2] = 0.0f; } else { hsv[2] = 1.0f; } pixel = Color.HSVToColor(hsv);
         */

        buffOut.put(pixel);
    }

    buffOut.rewind();

    base.copyPixelsFromBuffer(buffOut);
    blend.recycle();

    return base;
}

From source file:org.telegram.ui.GroupCreateActivity.java

public Emoji.XImageSpan createAndPutChipForUser(TLRPC.User user) {
    LayoutInflater lf = (LayoutInflater) parentActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View textView = lf.inflate(R.layout.group_create_bubble, null);
    TextView text = (TextView) textView.findViewById(R.id.bubble_text_view);
    text.setText(Utilities.formatName(user.first_name, user.last_name));

    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(spec, spec);/*from   w  w  w  .j  a  va 2s .  com*/
    textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(b);
    canvas.translate(-textView.getScrollX(), -textView.getScrollY());
    textView.draw(canvas);
    textView.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = textView.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    textView.destroyDrawingCache();

    final BitmapDrawable bmpDrawable = new BitmapDrawable(b);
    bmpDrawable.setBounds(0, 0, b.getWidth(), b.getHeight());

    SpannableStringBuilder ssb = new SpannableStringBuilder("");
    Emoji.XImageSpan span = new Emoji.XImageSpan(bmpDrawable, ImageSpan.ALIGN_BASELINE);
    allSpans.add(span);
    selectedContacts.put(user.id, span);
    for (ImageSpan sp : allSpans) {
        ssb.append("<<");
        ssb.setSpan(sp, ssb.length() - 2, ssb.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    userSelectEditText.setText(ssb);
    userSelectEditText.setSelection(ssb.length());
    return span;
}

From source file:com.fastbootmobile.encore.app.fragments.AlbumViewFragment.java

/**
 * Sets the arguments for this fragment// w  w w.j a  va2s .  c  o m
 * @param hero The hero header image bitmap
 * @param extras The extras contained in the Intent that started the parent activity
 */
public void setArguments(Bitmap hero, Bundle extras) {
    if (hero != null) {
        mHeroImage = hero.copy(hero.getConfig(), false);
    }
    mBackgroundColor = extras.getInt(AlbumActivity.EXTRA_BACKGROUND_COLOR, 0xFF333333);

    String albumRef = extras.getString(AlbumActivity.EXTRA_ALBUM);
    ProviderIdentifier provider = ProviderIdentifier
            .fromSerialized(extras.getString(AlbumActivity.EXTRA_PROVIDER));
    mAlbum = ProviderAggregator.getDefault().retrieveAlbum(albumRef, provider);

    if (mAlbum == null) {
        Log.e(TAG, "Album isn't in cache and provider returned null!");
    }
}

From source file:io.jawg.osmcontributor.ui.utils.BitmapHandler.java

/**
 * Get the white icon corresponding to a poiType.
 *
 * @param poiType the PoiType or null for notes.
 * @return The white icon.// w  ww.  j a  v  a 2 s .  c o  m
 */
public Drawable getIconWhite(PoiType poiType) {
    Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(),
            poiType == null ? R.drawable.open_book : getIconDrawableId(poiType));
    myBitmap = myBitmap.copy(myBitmap.getConfig(), true);

    int[] allpixels = new int[myBitmap.getHeight() * myBitmap.getWidth()];

    myBitmap.getPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

    for (int i = 0; i < myBitmap.getHeight() * myBitmap.getWidth(); i++) {
        if (allpixels[i] != 0) {
            int A = Color.alpha(allpixels[i]);
            // inverting byte for each R/G/B channel
            int R = 255 - Color.red(allpixels[i]);
            int G = 255 - Color.green(allpixels[i]);
            int B = 255 - Color.blue(allpixels[i]);
            // set newly-inverted pixel to output image
            allpixels[i] = Color.argb(A, R, G, B);
        }
    }

    myBitmap.setPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

    return new BitmapDrawable(context.getResources(), myBitmap);
}

From source file:org.amahi.anywhere.service.AudioService.java

private Bitmap getAudioPlayerRemoteArtwork(Bitmap audioAlbumArt) {
    if (audioAlbumArt == null) {
        return null;
    }//from  ww  w .j  a v  a 2 s  .co  m

    Bitmap.Config artworkConfig = audioAlbumArt.getConfig();

    if (artworkConfig == null) {
        artworkConfig = Bitmap.Config.ARGB_8888;
    }

    return audioAlbumArt.copy(artworkConfig, false);
}

From source file:com.berniesanders.fieldthebern.views.MapScreenView.java

private Bitmap colorBitmap(final Bitmap bm, int color) {

    Paint paint = new Paint();
    ColorFilter filter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN);
    paint.setColorFilter(filter);/*from   www.  ja  v  a2s.  com*/
    Bitmap copiedBitmap = bm.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(copiedBitmap);
    canvas.drawBitmap(bm, 0, 0, paint);
    return copiedBitmap;
}

From source file:com.watasan.infospider.fragment.PrimitiveFragment.java

public void drawProfileImage(CApiParams params) {
    // UserId??//  ww w  .jav  a2  s . c o m
    userId = params.getScreenName();
    // ?Ids??
    ItemBase item = ItemListManager.getItem(params.getScreenName());
    if (item == null)
        return;

    byte[] data = null;
    data = ((ItemIds) item).getProfileImageData();
    if (data != null) {
        Log.v("--image--", "data= " + params.getImageIndex() + "  type= " + params.getApiType());
        int width = ((ItemIds) item).getProfileImageDataWidth();
        int height = ((ItemIds) item).getProfileImageDataHeight();
        Bitmap bitmap = null;
        if (width == 0 && height == 0) {
            bitmap = ImageUtil.createBitmapFromByteArrayOfResource(data);
        } else {
            bitmap = ImageUtil.createBitmapFromByteArrayOfBitmap(data, width, height);
        }

        setImage(bitmap.copy(Bitmap.Config.ARGB_4444, true));
        // ?
        data = null;
        if (bitmap.isRecycled())
            bitmap.recycle();
        bitmap = null;
    } else {
        Log.v("------- Image index", "NULL data--index =" + params.getImageIndex());
    }
}

From source file:com.yahala.ui.GroupCreateActivity.java

public XImageSpan createAndPutChipForUser(TLRPC.User user) {
    LayoutInflater lf = (LayoutInflater) parentActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View textView = lf.inflate(R.layout.group_create_bubble, null);
    TextView text = (TextView) textView.findViewById(R.id.bubble_text_view);
    String name = Utilities.formatName(user.first_name, user.last_name);
    if (name.length() == 0 && user.phone != null && user.phone.length() != 0) {
        name = PhoneFormat.getInstance().format("+" + user.phone);
    }/*from  w  w w .  j  a  va2  s.c  o m*/
    text.setText(name + ", ");

    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(spec, spec);
    textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(b);
    canvas.translate(-textView.getScrollX(), -textView.getScrollY());
    textView.draw(canvas);
    textView.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = textView.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    textView.destroyDrawingCache();

    final BitmapDrawable bmpDrawable = new BitmapDrawable(b);
    bmpDrawable.setBounds(0, 0, b.getWidth(), b.getHeight());

    SpannableStringBuilder ssb = new SpannableStringBuilder("");
    XImageSpan span = new XImageSpan(bmpDrawable, ImageSpan.ALIGN_BASELINE);
    allSpans.add(span);
    selectedContacts.put(user.jid, span);
    for (ImageSpan sp : allSpans) {
        ssb.append("<<");
        ssb.setSpan(sp, ssb.length() - 2, ssb.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    userSelectEditText.setText(ssb);
    userSelectEditText.setSelection(ssb.length());
    return span;
}