本文整理汇总了Java中android.graphics.Bitmap.compress方法的典型用法代码示例。如果您正苦于以下问题:Java Bitmap.compress方法的具体用法?Java Bitmap.compress怎么用?Java Bitmap.compress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.graphics.Bitmap
的用法示例。
在下文中一共展示了Bitmap.compress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: init
import android.graphics.Bitmap; //导入方法依赖的package包/类
@Override
public Point init(Context context, Uri uri) throws Exception {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
options.inJustDecodeBounds = false;
ContentResolver resolver = context.getContentResolver();
InputStream inputStream = resolver.openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
String filename = String.valueOf(uri.toString().hashCode());
cacheFile = new File(context.getCacheDir(), filename);
FileOutputStream fileOutputStream = new FileOutputStream(cacheFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, JPEG_QUALITY, fileOutputStream);
decoder = new CustomRegionDecoder();
return decoder.init(context, Uri.fromFile(cacheFile));
}
示例2: inputPicTosdcard
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* 将图片写入sd卡
*
*/
public static void inputPicTosdcard(Bitmap bitmap, String picName) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
File SDFile = Environment.getExternalStorageDirectory();
File destDir = new File(SDFile.getAbsolutePath() + "/qqcyData/");//文件目录
if (!destDir.exists()) {//判断目录是否存在,不存在创建
destDir.mkdir();//创建目录
}
File pic = new File(destDir.getPath() + File.separator +picName);
if (!pic.exists()) {
try {
pic.createNewFile();//创建文件
FileOutputStream outputStream = new FileOutputStream(pic, true);
outputStream.write(baos.toByteArray());//写入内容
outputStream.close();//关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
示例3: getStreamFromContent
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* Retrieves {@link InputStream} of image by URI (image is accessed using {@link ContentResolver}).
*
* @param imageUri Image URI
* @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object)
* DisplayImageOptions.extraForDownloader(Object)}; can be null
* @return {@link InputStream} of image
* @throws FileNotFoundException if the provided URI could not be opened
*/
protected InputStream getStreamFromContent(String imageUri, Object extra) throws FileNotFoundException {
ContentResolver res = context.getContentResolver();
Uri uri = Uri.parse(imageUri);
if (isVideoContentUri(uri)) { // video thumbnail
Long origId = Long.valueOf(uri.getLastPathSegment());
Bitmap bitmap = MediaStore.Video.Thumbnails
.getThumbnail(res, origId, MediaStore.Images.Thumbnails.MINI_KIND, null);
if (bitmap != null) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, bos);
return new ByteArrayInputStream(bos.toByteArray());
}
} else if (imageUri.startsWith(CONTENT_CONTACTS_URI_PREFIX)) { // contacts photo
return getContactPhotoStream(uri);
}
return res.openInputStream(uri);
}
示例4: attachImage
import android.graphics.Bitmap; //导入方法依赖的package包/类
private void attachImage(TaskEntity task, Bitmap image) {
if (task == null || image == null) return;
ByteArrayOutputStream out = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 50, out);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
task.setImage(in);
try {
task.save();
} catch (CouchbaseLiteException e) {
Log.e(Application.TAG, "Cannot attach image", e);
}
}
示例5: save
import android.graphics.Bitmap; //导入方法依赖的package包/类
public boolean save(String imageUri, Bitmap bitmap) throws IOException {
File imageFile = getFile(imageUri);
File tmpFile = new File(imageFile.getAbsolutePath() + TEMP_IMAGE_POSTFIX);
OutputStream os = new BufferedOutputStream(new FileOutputStream(tmpFile), this.bufferSize);
boolean savedSuccessfully = false;
try {
savedSuccessfully = bitmap.compress(this.compressFormat, this.compressQuality, os);
bitmap.recycle();
return savedSuccessfully;
} finally {
IoUtils.closeSilently(os);
if (savedSuccessfully && !tmpFile.renameTo(imageFile)) {
savedSuccessfully = false;
}
if (!savedSuccessfully) {
tmpFile.delete();
}
}
}
示例6: compressBitmap
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* 简单的压缩图片
* 图片对象有可能会很大, 但是转成成 bytes之后, 会很小, 此方法会判断不准确
*/
@Deprecated
public static Bitmap compressBitmap(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 90;
while (baos.toByteArray().length / 1024 > 200) { // 循环判断如果压缩后图片是否大于200kb,大于继续压缩
baos.reset(); // 重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
options -= 10;// 每次都减少10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
return bitmap;
}
示例7: rotate
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* Rotates a bitmat of the given degrees
* @param bmp
* @param degrees
* @return
*/
public static Bitmap rotate(Bitmap bmp, int degrees, boolean reflex) {
if (bmp==null) throw new NullPointerException();
//getting scales of the image
int width = bmp.getWidth();
int height = bmp.getHeight();
//Creating a Matrix and rotating it to specified degrees
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
if (reflex) matrix.postScale(-1, 1);
//Getting the rotated Bitmap
Bitmap rotatedBmp = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
rotatedBmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
return rotatedBmp;
}
示例8: getStreamFromVideo
import android.graphics.Bitmap; //导入方法依赖的package包/类
private InputStream getStreamFromVideo(ImageDownloadInfo imageDownloadInfo) throws IOException {
try {
if (imageDownloadInfo.getExtraInfo() < MediaStore.Images.Thumbnails.MINI_KIND || imageDownloadInfo.getExtraInfo() > MediaStore.Video.Thumbnails.MICRO_KIND) {
imageDownloadInfo.setExtraInfo(MediaStore.Images.Thumbnails.MINI_KIND);
}
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(imageDownloadInfo.getImageUrl(), imageDownloadInfo.getExtraInfo());
ByteArrayOutputStream os = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, os);
return new ByteArrayInputStream(os.toByteArray());
} catch (Exception e) {
Log.e("error", "e: " + e.toString());
}
return null;
}
示例9: getCompressedImage
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* 获取压缩后图片的二进制数据
* @param srcPath
* @return
*/
public byte[] getCompressedImage(String srcPath) {
if (!new File(srcPath).exists()){
return null;
}
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//此时返回bm为空
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = 800f;//这里设置高度为800f
float ww = 480f;//这里设置宽度为480f
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0) {
be = 1;
}
newOpts.inSampleSize = be;//设置缩放比例
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 300) { //循环判断如果压缩后图片是否大于300kb,大于继续压缩
baos.reset();//重置baos即清空baos
options -= 15;//每次都减少15
bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
}
return baos.toByteArray();
}
示例10: bundleThumbnail
import android.graphics.Bitmap; //导入方法依赖的package包/类
static void bundleThumbnail(PlanarYUVLuminanceSource source, Bundle bundle) {
int[] pixels = source.renderThumbnail();
int width = source.getThumbnailWidth();
int height = source.getThumbnailHeight();
Bitmap bitmap = Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);
bundle.putByteArray(DecodeThread.BARCODE_BITMAP, out.toByteArray());
}
示例11: takeScreenshot
import android.graphics.Bitmap; //导入方法依赖的package包/类
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return;
}
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();
}
}
示例12: saveTempPicture
import android.graphics.Bitmap; //导入方法依赖的package包/类
public static void saveTempPicture(String filePath,String fileTempPath){
File file = new File(fileTempPath);
Bitmap bmp = PictureUtil.getSmallBitmap(filePath,120,120);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 把压缩后的数据存放到baos中
bmp.compress(Bitmap.CompressFormat.PNG, 10, baos);
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
示例13: cacheFavicon
import android.graphics.Bitmap; //导入方法依赖的package包/类
private void cacheFavicon(Bitmap icon) {
String hash = String.valueOf(Utils.getDomainName(getUrl()).hashCode());
Log.d(Constants.TAG, "Caching icon for " + Utils.getDomainName(getUrl()));
File image = new File(mActivity.getCacheDir(), hash + ".png");
try {
FileOutputStream fos = new FileOutputStream(image);
icon.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
示例14: setImageFromBitmap
import android.graphics.Bitmap; //导入方法依赖的package包/类
@Override
public void setImageFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageData = stream.toByteArray();
setBinaryData(imageData);
setMimeType(ImageFormats.getMimeTypeForBinarySignature(imageData));
setDescription("");
setPictureType(PictureTypes.DEFAULT_ID);
}
示例15: Bitmap2Bytes
import android.graphics.Bitmap; //导入方法依赖的package包/类
private static byte[] Bitmap2Bytes(Bitmap bm) {
if (bm == null) {
return null;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}