public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.wrapper_cn_image_cropper); cropImageView = (CropImageView) findViewById(R.id.crop_imageview); // Sets initial aspect ratio to 10/10, for demonstration purposes //cropImageView.setAspectRatio(DEFAULT_ASPECT_RATIO_VALUES, DEFAULT_ASPECT_RATIO_VALUES); Intent intent = getIntent(); isCrop = intent.getBooleanExtra(EXTRA_CROP, true); aspectX = intent.getIntExtra(EXTRA_ASPECTX, 0); aspectY = intent.getIntExtra(EXTRA_ASPECTY, 0); outputX = intent.getIntExtra(EXTRA_OUTPUTX, 0); outputY = intent.getIntExtra(EXTRA_OUTPUTY, 0); isScale = intent.getBooleanExtra(EXTRA_SCALE, true); output = intent.getParcelableExtra(EXTRA_OUTPUT); isReturnData = intent.getBooleanExtra(EXTRA_RETURN_DATA, false); noFaceDetection = intent.getBooleanExtra(EXTRA_NOFACEDETECTION, true); outputFormat = intent.getStringExtra(EXTRA_OUTPUTFORMAT); if (TextUtils.isEmpty(outputFormat)) { outputFormat = Bitmap.CompressFormat.PNG.toString(); } title = intent.getStringExtra(EXTRA_TITLE); String path = output.getPath(); mSourceBitmap = BitmapFactory.decodeFile(path); cropImageView.setImageBitmap(mSourceBitmap); }
protected void onPostExecute(Drawable d) { if (d != null) { if (imgView != null) imgView.setImageDrawable(d); BitmapDrawable bd = (BitmapDrawable) d; Bitmap bmp = bd.getBitmap(); ByteArrayOutputStream s = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.JPEG, 100, s); if (mFIsThumbnail) m_imgThumb = s.toByteArray(); else m_imgData = s.toByteArray(); if (m_icc != null) m_icc.imgCompleted(MFBImageInfo.this); } }
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) { ByteArrayOutputStream output = new ByteArrayOutputStream(); bmp.compress(CompressFormat.PNG, 100, output); if (needRecycle) { bmp.recycle(); } byte[] result = output.toByteArray(); try { output.close(); } catch (Exception e) { e.printStackTrace(); } return result; }
final ShareParams shareDataToShareParams(Platform plat) { if (plat == null || shareParamsMap == null) { toast("ssdk_oks_share_failed"); return null; } try { String imagePath = ResHelper.forceCast(shareParamsMap.get("imagePath")); Bitmap viewToShare = ResHelper.forceCast(shareParamsMap.get("viewToShare")); if (TextUtils.isEmpty(imagePath) && viewToShare != null && !viewToShare.isRecycled()) { String path = ResHelper.getCachePath(plat.getContext(), "screenshot"); File ss = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg"); FileOutputStream fos = new FileOutputStream(ss); viewToShare.compress(CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); shareParamsMap.put("imagePath", ss.getAbsolutePath()); } } catch (Throwable t) { t.printStackTrace(); toast("ssdk_oks_share_failed"); return null; } return new ShareParams(shareParamsMap); }
final ShareParams shareDataToShareParams(Platform plat) { if (plat == null || shareParamsMap == null) { toast("ssdk_oks_share_failed"); return null; } try { String imagePath = ResHelper.forceCast(shareParamsMap.get("imagePath")); Bitmap viewToShare = ResHelper.forceCast(shareParamsMap.get("viewToShare")); if (TextUtils.isEmpty(imagePath) && viewToShare != null && !viewToShare.isRecycled()) { String path = ResHelper.getCachePath(MobSDK.getContext(), "screenshot"); File ss = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg"); FileOutputStream fos = new FileOutputStream(ss); viewToShare.compress(CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); shareParamsMap.put("imagePath", ss.getAbsolutePath()); } } catch (Throwable t) { t.printStackTrace(); toast("ssdk_oks_share_failed"); return null; } return new ShareParams(shareParamsMap); }
/** * 将bitmap对象转换成byte数组 * @param bmp * @param needRecycle * @return */ public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) { ByteArrayOutputStream output = new ByteArrayOutputStream(); bmp.compress(CompressFormat.PNG, 100, output); if (needRecycle) { bmp.recycle(); } byte[] result = output.toByteArray(); try { output.close(); } catch (Exception e) { e.printStackTrace(); } return result; }
/** * 按质量压缩 * * @param src 源图片 * @param maxByteSize 允许最大值字节数 * @param recycle 是否回收 * @return 质量压缩压缩过的图片 */ public static Bitmap compressByQuality(Bitmap src, long maxByteSize, boolean recycle) { if (isEmptyBitmap(src) || maxByteSize <= 0) { return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); int quality = 100; src.compress(CompressFormat.JPEG, quality, baos); while (baos.toByteArray().length > maxByteSize && quality > 0) { baos.reset(); src.compress(CompressFormat.JPEG, quality -= 5, baos); } if (quality < 0) { return null; } byte[] bytes = baos.toByteArray(); if (recycle && !src.isRecycled()) { src.recycle(); } return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); }
/** * 将图片保存在指定路径中 * * @param bitmap * @param descPath */ public static void saveBitmap(Bitmap bitmap, String descPath) { File file = new File(descPath); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } if (!file.exists()) { try { bitmap.compress(CompressFormat.JPEG, 30, new FileOutputStream( file)); } catch (FileNotFoundException e) { e.printStackTrace(); } } if (null != bitmap) { bitmap.recycle(); bitmap = null; } }
/** * 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); }
public static File getScaledImageFileWithMD5(File imageFile, String mimeType) { String filePath = imageFile.getPath(); if (!isInvalidPictureFile(mimeType)) { return null; } String tempFilePath = getTempFilePath(FileUtil.getExtensionName(filePath)); File tempImageFile = AttachmentStore.create(tempFilePath); if (tempImageFile == null) { return null; } CompressFormat compressFormat = CompressFormat.JPEG; // 压缩数值由第三方开发者自行决定 int maxWidth = 720; int quality = 60; if (ImageUtil.scaleImage(imageFile, tempImageFile, maxWidth, compressFormat, quality)) { return tempImageFile; } else { return null; } }
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) { ByteArrayOutputStream output = new ByteArrayOutputStream(); bmp.compress(CompressFormat.JPEG, 50, output); if (needRecycle) { bmp.recycle(); } byte[] result = output.toByteArray(); try { output.close(); } catch (Exception e) { e.printStackTrace(); } return result; }
public static void saveImage(Context context, String fileName, Bitmap bitmap, int quality) throws IOException { if (bitmap == null || fileName == null || context == null) return; FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, quality, stream); byte[] bytes = stream.toByteArray(); fos.write(bytes); fos.close(); }
protected static final String a(Bitmap bitmap, String str, String str2) { File file = new File(str); if (!file.exists()) { file.mkdirs(); } String stringBuffer = new StringBuffer(str).append(str2).toString(); File file2 = new File(stringBuffer); if (file2.exists()) { file2.delete(); } if (bitmap != null) { try { OutputStream fileOutputStream = new FileOutputStream(file2); bitmap.compress(CompressFormat.JPEG, 80, fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); bitmap.recycle(); return stringBuffer; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } } return null; }
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public static byte[] convertBitmapToBytes(Bitmap bitmap) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount()); bitmap.copyPixelsToBuffer(buffer); return buffer.array(); } else { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] data = baos.toByteArray(); return data; } }
public static File getScaledImageFileWithMD5(File imageFile, String mimeType) { String filePath = imageFile.getPath(); if (!isInvalidPictureFile(mimeType)) { LogUtil.i("ImageUtil", "is invalid picture file"); return null; } String tempFilePath = getTempFilePath(FileUtil.getExtensionName(filePath)); File tempImageFile = AttachmentStore.create(tempFilePath); if (tempImageFile == null) { return null; } CompressFormat compressFormat = CompressFormat.JPEG; // 压缩数值由第三方开发者自行决定 int maxWidth = 720; int quality = 60; if (ImageUtil.scaleImage(imageFile, tempImageFile, maxWidth, compressFormat, quality)) { return tempImageFile; } else { return null; } }
public static byte[] convertBitmap(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, 100, baos); int options = 100; // LogUtil.e("===baos.toByteArray().length===" + baos.toByteArray().length); // LogUtil.e("===baos.size===" + baos.size()); while (baos.size() / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩 baos.reset();// 重置baos即清空baos options -= 10;// 每次都减少10 bitmap.compress(CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中 } try { baos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (bitmap != null) { bitmap.recycle(); bitmap = null; } } return baos.toByteArray(); }
private String outputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException { // Some content: URIs do not map to file paths (e.g. picasa). String realPath = FileHelper.getRealPath(uri, this.cordova); // Get filename from uri String fileName = realPath != null ? realPath.substring(realPath.lastIndexOf('/') + 1) : "modified." + (this.encodingType == JPEG ? "jpg" : "png"); String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); //String fileName = "IMG_" + timeStamp + (this.encodingType == JPEG ? ".jpg" : ".png"); String modifiedPath = getTempDirectoryPath() + "/" + fileName; OutputStream os = new FileOutputStream(modifiedPath); CompressFormat compressFormat = this.encodingType == JPEG ? CompressFormat.JPEG : CompressFormat.PNG; bitmap.compress(compressFormat, this.mQuality, os); os.close(); if (exifData != null && this.encodingType == JPEG) { try { if (this.correctOrientation && this.orientationCorrected) { exifData.resetOrientation(); } exifData.createOutFile(modifiedPath); exifData.writeExifData(); exifData = null; } catch (IOException e) { e.printStackTrace(); } } return modifiedPath; }
/** * Compress bitmap using jpeg, convert to Base64 encoded string, and return to JavaScript. * * @param bitmap */ public void processPicture(Bitmap bitmap, int encodingType) { ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream(); CompressFormat compressFormat = encodingType == JPEG ? CompressFormat.JPEG : CompressFormat.PNG; try { if (bitmap.compress(compressFormat, mQuality, jpeg_data)) { byte[] code = jpeg_data.toByteArray(); byte[] output = Base64.encode(code, Base64.NO_WRAP); String js_out = new String(output); this.callbackContext.success(js_out); js_out = null; output = null; code = null; } } catch (Exception e) { this.failPicture("Error compressing image."); } jpeg_data = null; }
public final void setThumbImage(Bitmap bitmap) { try { OutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, 85, byteArrayOutputStream); this.thumbData = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.close(); } catch (Exception e) { e.printStackTrace(); a.a(TAG, "put thumb failed"); } }
/** * 按质量压缩 * * @param src 源图片 * @param quality 质量 * @param recycle 是否回收 * @return 质量压缩后的图片 */ public static Bitmap compressByQuality(Bitmap src, int quality, boolean recycle) { if (isEmptyBitmap(src) || quality < 0 || quality > 100) return null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); src.compress(CompressFormat.JPEG, quality, baos); byte[] bytes = baos.toByteArray(); if (recycle && !src.isRecycled()) src.recycle(); return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); }
public boolean cropBitmap(int whichWallpaper) { Bitmap crop = mStreamProvider.readCroppedBitmap( mCropBounds, mOutWidth, mOutHeight, mRotation); if (crop == null) { return false; } boolean failure = false; // Compress to byte array ByteArrayOutputStream tmpOut = new ByteArrayOutputStream(2048); if (crop.compress(CompressFormat.JPEG, DEFAULT_COMPRESS_QUALITY, tmpOut)) { // Set the wallpaper try { byte[] outByteArray = tmpOut.toByteArray(); WallpaperManagerCompat.getInstance(mContext).setStream( new ByteArrayInputStream(outByteArray), null, true, whichWallpaper); if (mOnBitmapCroppedHandler != null) { mOnBitmapCroppedHandler.onBitmapCropped(outByteArray); } } catch (IOException e) { Log.w(TAG, "cannot write stream to wallpaper", e); failure = true; } } else { Log.w(TAG, "cannot compress bitmap"); failure = true; } return !failure; // True if any of the operations failed }
private void b(Context context, String str, String str2, Bitmap bitmap, int i, k kVar) { Object wXEmojiObject = new WXEmojiObject(); OutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 100, byteArrayOutputStream); byteArrayOutputStream.flush(); wXEmojiObject.emojiData = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.close(); WXMediaMessage wXMediaMessage = new WXMediaMessage(); wXMediaMessage.title = str; wXMediaMessage.mediaObject = wXEmojiObject; wXMediaMessage.description = str2; wXMediaMessage.thumbData = a(context, wXEmojiObject.emojiData); a(wXMediaMessage, "emoji", i, kVar); }
private static InputStream openBitmapStream(CompressFormat format, int width, int height) { Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); ByteArrayOutputStream os = new ByteArrayOutputStream(); bitmap.compress(format, 100 /*quality*/, os); bitmap.recycle(); byte[] data = os.toByteArray(); return new ByteArrayInputStream(data); }
@TargetApi(Build.VERSION_CODES.FROYO) private InputStream getVideoThumbnailStream(String filePath) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { Bitmap bitmap = ThumbnailUtils .createVideoThumbnail(filePath, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND); if (bitmap != null) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 0, bos); return new ByteArrayInputStream(bos.toByteArray()); } } return null; }
public WXImageObject(Bitmap bitmap) { try { OutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, 85, byteArrayOutputStream); this.imageData = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.close(); } catch (Throwable e) { Ln.e(e); } }
public final void setThumbImage(Bitmap bitmap) { try { OutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, 85, byteArrayOutputStream); this.thumbData = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.close(); } catch (Throwable e) { Ln.e(e); Ln.e("put thumb failed", new Object[0]); } }
private String a(Bitmap bitmap, a aVar) { try { File createTempFile = File.createTempFile("bm_tmp", ".png"); OutputStream fileOutputStream = new FileOutputStream(createTempFile); bitmap.compress(CompressFormat.PNG, 100, fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); return a(createTempFile.getAbsolutePath(), aVar); } catch (Throwable th) { Ln.e(th); return null; } }
private void cacheStoreDrawable(String key, Bitmap bitmap) { if (isDrawableInCache(key)) return; File drawableFile = cacheGetFileName(key); try (FileOutputStream fos = new FileOutputStream(drawableFile)) { bitmap.compress(CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * 把batmap 转file * @param bitmap * @param filepath */ public static File saveBitmapFile(Bitmap bitmap, String filepath){ File file=new File(filepath);//将要保存图片的路径 try { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); bitmap.compress(CompressFormat.JPEG, 100, bos); bos.flush(); bos.close(); } catch (IOException e) { e.printStackTrace(); } return file; }
/** * 按质量压缩 * * @param src 源图片 * @param maxByteSize 允许最大值字节数 * @param recycle 是否回收 * @return 质量压缩压缩过的图片 */ public static Bitmap compressByQuality(Bitmap src, long maxByteSize, boolean recycle) { if (isEmptyBitmap(src) || maxByteSize <= 0) return null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); int quality = 100; src.compress(CompressFormat.JPEG, quality, baos); while (baos.toByteArray().length > maxByteSize && quality > 0) { baos.reset(); src.compress(CompressFormat.JPEG, quality -= 5, baos); } if (quality < 0) return null; byte[] bytes = baos.toByteArray(); if (recycle && !src.isRecycled()) src.recycle(); return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); }
/** * 按采样大小压缩 * * @param src 源图片 * @param sampleSize 采样率大小 * @param recycle 是否回收 * @return 按采样率压缩后的图片 */ public static Bitmap compressBySampleSize(Bitmap src, int sampleSize, boolean recycle) { if (isEmptyBitmap(src)) return null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = sampleSize; ByteArrayOutputStream baos = new ByteArrayOutputStream(); src.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] bytes = baos.toByteArray(); if (recycle && !src.isRecycled()) src.recycle(); return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options); }
/** * 按质量压缩 * * @param src 源图片 * @param quality 质量 * @param recycle 是否回收 * @return 质量压缩后的图片 */ public static Bitmap compressByQuality(final Bitmap src, @IntRange(from = 0, to = 100) final int quality, final boolean recycle) { if (isEmptyBitmap(src)) return null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); src.compress(CompressFormat.JPEG, quality, baos); byte[] bytes = baos.toByteArray(); if (recycle && !src.isRecycled()) src.recycle(); return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); }