Example usage for android.graphics Bitmap compress

List of usage examples for android.graphics Bitmap compress

Introduction

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

Prototype

@WorkerThread
public boolean compress(CompressFormat format, int quality, OutputStream stream) 

Source Link

Document

Write a compressed version of the bitmap to the specified outputstream.

Usage

From source file:mobisocial.musubi.ui.util.FeedHTML.java

public static void writeObj(FileOutputStream fo, Context context, IdentitiesManager identitiesManager,
        MObject object) {/*w ww  . j  a  va2s.c o m*/
    //TODO: it would be better to put the export code inside the obj handlers
    MIdentity ident = identitiesManager.getIdentityForId(object.identityId_);
    if (ident == null)
        return;
    PrintWriter w = new PrintWriter(fo);
    w.print("<div>");
    w.print("<div style=\"float:left\">");

    w.print("<img src=\"data:image/jpeg;base64,");
    Bitmap thumb = UiUtil.safeGetContactThumbnail(context, identitiesManager, ident);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    thumb.compress(CompressFormat.JPEG, 90, bos);
    w.print(Base64.encodeToString(bos.toByteArray(), Base64.DEFAULT));
    w.print("\">");

    w.print("</div>");
    w.print("<div>");
    w.print("<h6>");
    w.print(UiUtil.safeNameForIdentity(ident));
    w.print("</h6>");

    try {
        if (object.type_.equals(StatusObj.TYPE)) {
            w.print(new JSONObject(object.json_).getString(StatusObj.TEXT));
        } else if (object.type_.equals(PictureObj.TYPE)) {
            w.print("<img src=\"data:image/jpeg;base64,");
            w.print(Base64.encodeToString(object.raw_, Base64.DEFAULT));
            w.print("\">");
        } else {
            throw new RuntimeException("unsupported type " + object.type_);
        }

    } catch (Throwable t) {
        Log.e("HTML EXPORT", "failed to process obj", t);
        w.print("<i>only visibile in musubi</i>");
    }
    w.print("</div>");
    w.print("</div>");

    w.print("</body>");
    w.print("</html>");
    w.flush();
}

From source file:Main.java

/**
 * Export the given bitmap to the specified {@code File}
 * @param context the context//from   w w w.  ja  va 2  s . c  om
 * @param bitmap the bitmap to export
 * @param file the file in which to save the view
 */
public static void exportBitmapToFile(@NonNull final Context context, @NonNull final Bitmap bitmap,
        @NonNull final File file) {
    OutputStream outputStream = null;
    try {
        outputStream = new BufferedOutputStream(new FileOutputStream(file, false));
        bitmap.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY, outputStream);
        outputStream.flush();
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            //No Worries
        }
    }
}

From source file:com.polatic.pantaudki.home.HomeFragment.java

@Deprecated
public static String encodeTobase64(Bitmap image) {
    Bitmap img = image;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    img.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
    byte[] b = byteArrayOutputStream.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);

    return imageEncoded;
}

From source file:Main.java

public static String imgsToBase64(String filePath) {
    Bitmap bitmap = BitmapFactory.decodeFile(filePath);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    String lastName = filePath.substring(filePath.lastIndexOf(".") + 1);
    if (lastName.equals(Bitmap.CompressFormat.JPEG)) {
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    } else {/*from w  w  w .  j ava2  s  . c  o  m*/
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    }
    return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
}

From source file:Main.java

public static Bitmap decodeInSampleSize(Bitmap bitmap, int reqWidth, int reqHeight) {
    if (bitmap.getWidth() >= reqWidth * 2 && bitmap.getHeight() >= reqHeight * 2) {
        ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayBitmapStream);
        byte[] byteArray = byteArrayBitmapStream.toByteArray();
        bitmap = decodeInSampleSize(byteArray, reqWidth, reqHeight);
    }/*from   www  .  j a  va2  s.c  o  m*/
    return bitmap;
}

From source file:Main.java

public static void saveBitmapToCacheDir(Context context, Bitmap bitmap, String name) {
    try {//from   ww  w.  j  a  va2  s .co m
        File file = new File(context.getCacheDir(), name);
        if (file.exists()) {
            file.delete();
        }
        FileOutputStream fos = context.openFileOutput(name, MODE_PRIVATE);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static byte[] bitmap2Bytes(Bitmap bitmap, Bitmap.CompressFormat mCompressFormat,
        final boolean needRecycle) {
    byte[] result = null;
    ByteArrayOutputStream output = null;
    try {/* w  w w  .jav  a2  s.  c o  m*/
        output = new ByteArrayOutputStream();
        bitmap.compress(mCompressFormat, 100, output);
        result = output.toByteArray();
        if (needRecycle) {
            bitmap.recycle();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:Main.java

public static void saveBitmap(String filename, Bitmap bitmap) throws Exception {
    File file = new File(sAppContext.getCacheDir().getPath() + "/" + filename);
    if (file.exists()) {
        file.delete();//ww  w .jav a  2 s  . c  o m
    }
    file.createNewFile();
    FileOutputStream fos = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();
}

From source file:Main.java

public static byte[] compress(Bitmap bitmap, int persentage) {
    if (bitmap == null)
        return new byte[0];
    if (persentage <= 0)
        persentage = 0;/*from w w  w.j a va 2 s . c o  m*/
    else if (persentage > 100)
        persentage = 100;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, persentage, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static void writeBitmapToDisk(String url, Bitmap bmp, Context ctx, CompressFormat format) {
    FileOutputStream fos;/*from www .j a  v a 2s . c  om*/
    String fileName = constructFileName(url);
    try {
        if (bmp != null) {
            fos = new FileOutputStream(new File(ctx.getCacheDir(), fileName));
            bmp.compress(format, 75, fos);
            fos.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}