android.graphics.Bitmap.compress()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(156)

本文整理了Java中android.graphics.Bitmap.compress()方法的一些代码示例,展示了Bitmap.compress()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bitmap.compress()方法的具体详情如下:
包路径:android.graphics.Bitmap
类名称:Bitmap
方法名:compress

Bitmap.compress介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

代码示例来源:origin: stackoverflow.com

//create a file to write bitmap data
File f = new File(context.getCacheDir(), filename);
f.createNewFile();

//Convert bitmap to byte array
Bitmap bitmap = your bitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();

//write the bytes in file
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();

代码示例来源:origin: stackoverflow.com

view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
b.compress(CompressFormat.JPEG, 95, new FileOutputStream("/some/location/image.jpg"));

代码示例来源:origin: stackoverflow.com

// Assume block needs to be inside a Try/Catch block.
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
Integer counter = 0;
File file = new File(path, "FitnessGirl"+counter+".jpg"); // the File to save , append increasing numeric counter to prevent files from getting overwritten.
fOut = new FileOutputStream(file);

Bitmap pictureBitmap = getImageBitmap(myurl); // obtaining the Bitmap
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush(); // Not really required
fOut.close(); // do not forget to close the stream

MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());

代码示例来源:origin: CarGuo/GSYVideoPlayer

public static void saveBitmap(Bitmap bitmap) throws FileNotFoundException {
  if (bitmap != null) {
    File file = new File(FileUtils.getPath(), "GSY-" + System.currentTimeMillis() + ".jpg");
    OutputStream outputStream;
    outputStream = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    bitmap.recycle();
  }
}

代码示例来源:origin: wangdan/AisenWeiBo

@Override
public String workInBackground(Void... params) throws TaskException {
  try {
    Bitmap bitmap = BitmapDecoder.decodeSampledBitmapFromFile(path, SystemUtils.getScreenHeight(WeiboClientActivity.this), SystemUtils.getScreenHeight(WeiboClientActivity.this));
    bitmap = BitmapUtil.rotateBitmap(bitmap, 90);
    ByteArrayOutputStream outArray = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outArray);
    FileUtils.writeFile(new File(path), outArray.toByteArray());
  } catch (OutOfMemoryError e) {
    e.printStackTrace();
  }
  return path;
}

代码示例来源:origin: stackoverflow.com

private void storeImage(Bitmap image) {
  File pictureFile = getOutputMediaFile();
  if (pictureFile == null) {
    Log.d(TAG,
        "Error creating media file, check storage permissions: ");// e.getMessage());
    return;
  } 
  try {
    FileOutputStream fos = new FileOutputStream(pictureFile);
    image.compress(Bitmap.CompressFormat.PNG, 90, fos);
    fos.close();
  } catch (FileNotFoundException e) {
    Log.d(TAG, "File not found: " + e.getMessage());
  } catch (IOException e) {
    Log.d(TAG, "Error accessing file: " + e.getMessage());
  }  
}

代码示例来源:origin: lyft/scissors

@Override
  public void run() {
    OutputStream outputStream = null;
    try {
      file.getParentFile().mkdirs();
      outputStream = new FileOutputStream(file);
      bitmap.compress(format, quality, outputStream);
      outputStream.flush();
    } catch (final Throwable throwable) {
      if (BuildConfig.DEBUG) {
        Log.e(TAG, "Error attempting to save bitmap.", throwable);
      }
    } finally {
      closeQuietly(outputStream);
    }
  }
}, null);

代码示例来源:origin: stackoverflow.com

public void saveImage(Context context, Bitmap b,String name,String extension){
  name=name+"."+extension;
  FileOutputStream out;
  try {
    out = context.openFileOutput(name, Context.MODE_PRIVATE);
    b.compress(Bitmap.CompressFormat.JPEG, 90, out);
    out.close();
  } catch (Exception e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: stackoverflow.com

private void takeScreenshot() {
  Date now = new Date();
  android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

  try {
    // image naming and path  to include sd card  appending name you choose for file
    String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

    // create bitmap screen capture
    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    File imageFile = new File(mPath);

    FileOutputStream outputStream = new FileOutputStream(imageFile);
    int quality = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
    outputStream.flush();
    outputStream.close();

    openScreenshot(imageFile);
  } catch (Throwable e) {
    // Several error may come out with file handling or OOM
    e.printStackTrace();
  }
}

代码示例来源:origin: stackoverflow.com

Bitmap photo = (Bitmap) "your Bitmap image";
photo = Bitmap.createScaledBitmap(photo, 100, 100, false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

File f = new File(Environment.getExternalStorageDirectory()
    + File.separator + "Imagename.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();

代码示例来源:origin: stackoverflow.com

Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
byte[] b = baos.toByteArray();

代码示例来源:origin: stackoverflow.com

//First create a new URL object 
URL url = new URL("http://www.google.co.uk/logos/holiday09_2.gif")

//Next create a file, the example below will save to the SDCARD using JPEG format
File file = new File("/sdcard/example.jpg");

//Next create a Bitmap object and download the image to bitmap
Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());

//Finally compress the bitmap, saving to the file previously created
bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(file));

代码示例来源:origin: wangdan/AisenWeiBo

@Override
public String workInBackground(Void... params) throws TaskException {
  try {
    Bitmap bitmap = BitmapDecoder.decodeSampledBitmapFromFile(path, SystemUtils.getScreenHeight(getActivity()), SystemUtils.getScreenHeight(getActivity()));
    bitmap = BitmapUtil.rotateBitmap(bitmap, 90);
    
    ByteArrayOutputStream outArray = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 100, outArray);
    
    FileUtils.writeFile(new File(path), outArray.toByteArray());
  } catch (OutOfMemoryError e) {
    e.printStackTrace();
  }
  return path;
}

代码示例来源:origin: android10/Android-CleanArchitecture

/**
 * Cache an element.
 *
 * @param bitmap The bitmap to be put in the cache.
 * @param fileName A string representing the name of the file to be cached.
 */
synchronized void put(Bitmap bitmap, String fileName) {
 final File file = buildFileFromFilename(fileName);
 if (!file.exists()) {
  try {
   final FileOutputStream fileOutputStream = new FileOutputStream(file);
   bitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream);
   fileOutputStream.flush();
   fileOutputStream.close();
  } catch (IOException e) {
   Log.e(TAG, e.getMessage());
  }
 }
}

代码示例来源:origin: CarGuo/GSYVideoPlayer

public static void saveBitmap(Bitmap bitmap, File file) {
  if (bitmap != null) {
    OutputStream outputStream;
    try {
      outputStream = new FileOutputStream(file);
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
      bitmap.recycle();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }
}

代码示例来源:origin: chat-sdk/chat-sdk-android

public static File compressImageToFile(Bitmap bitmap, File outFile, Bitmap.CompressFormat format) {
  try {
    OutputStream outStream = new FileOutputStream(outFile);
    bitmap.compress(format, 100, outStream);
    outStream.flush();
    outStream.close();
  }
  catch (Exception e) {
    ChatSDK.logError(e);
    return null;
  }
  return outFile;
}

代码示例来源:origin: stackoverflow.com

private void SaveImage(Bitmap finalBitmap) {

  String root = Environment.getExternalStorageDirectory().toString();
  File myDir = new File(root + "/saved_images");    
  myDir.mkdirs();
  Random generator = new Random();
  int n = 10000;
  n = generator.nextInt(n);
  String fname = "Image-"+ n +".jpg";
  File file = new File (myDir, fname);
  if (file.exists ()) file.delete (); 
  try {
      FileOutputStream out = new FileOutputStream(file);
      finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
      out.flush();
      out.close();

  } catch (Exception e) {
      e.printStackTrace();
  }
}

代码示例来源:origin: crazycodeboy/TakePhoto

@Override
public void run() {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  int options = 100;
  bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//质量压缩方法,把压缩后的数据存放到baos中 (100表示不压缩,0表示压缩到最小)
  while (baos.toByteArray().length > config.getMaxSize()) {//循环判断如果压缩后图片是否大于指定大小,大于继续压缩
      options = 5;//如果图片质量小于5,为保证压缩后的图片质量,图片最底压缩质量为5
    bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//将压缩后的图片保存到baos中
    if (options == 5) {
      break;//如果图片的质量已降到最低则,不再进行压缩
    File thumbnailFile = getThumbnailFile(new File(imgPath));
    FileOutputStream fos = new FileOutputStream(thumbnailFile);//将压缩后的图片保存的本地上指定路径中
    fos.write(baos.toByteArray());
    fos.flush();
    fos.close();
    sendMsg(true, thumbnailFile.getPath(), null, listener);
  } catch (Exception e) {

代码示例来源:origin: stackoverflow.com

public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
  ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
  image.compress(compressFormat, quality, byteArrayOS);
  return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}

public static Bitmap decodeBase64(String input)
{
  byte[] decodedBytes = Base64.decode(input, 0);
  return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}

相关文章