public static Bitmap decodeSampledBitmapFromDescriptor(FileDescriptor fileDescriptor) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options); final int screenWidth = SystemUtils.getScreenWidth(); final int screenHeight = SystemUtils.getScreenHeight(); float reqWidth = screenWidth * 1.5f; float reqHeight = screenHeight; if (reqWidth > options.outWidth) reqHeight = reqWidth * 1.0f / options.outWidth * options.outHeight; if (reqHeight > 3000) reqHeight = 3000; return decodeSampledBitmapFromDescriptor(fileDescriptor, (int) reqWidth, (int) reqHeight); }
public static Bitmap decodeSampledBitmapFromByte(byte[] data) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(data, 0, data.length, options); final int screenWidth = SystemUtils.getScreenWidth(); final int screenHeight = SystemUtils.getScreenHeight(); float reqWidth = screenWidth; float reqHeight = screenHeight; if (reqWidth > options.outWidth) reqHeight = reqWidth * 1.0f / options.outWidth * options.outHeight; if (reqHeight > 3000) reqHeight = 3000; return decodeSampledBitmapFromByte(data, (int) reqWidth, (int) reqHeight); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ui_picture_pager); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayShowHomeEnabled(false); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } mBean = savedInstanceState == null ? PhotosFragment.photos : (PhotosBean) savedInstanceState.getSerializable("bean"); index = savedInstanceState == null ? getIntent().getIntExtra("index", 0) : savedInstanceState.getInt("index", 0); myViewPagerAdapter = new MyViewPagerAdapter(getFragmentManager()); viewPager.setAdapter(myViewPagerAdapter); viewPager.setCurrentItem(index); viewPager.setOnPageChangeListener(this); if (size() > 1 && getSupportActionBar() != null) getSupportActionBar().setTitle(String.format("%d/%d", index + 1, size())); layStatus.setVisibility(View.VISIBLE); viewStatus.setVisibility(View.VISIBLE); txtStatus.setText(getStatus(index).getText()); if (Build.VERSION.SDK_INT >= 19) { layToolbar.setPadding( 0, layToolbar.getPaddingTop() + SystemUtils.getStatusBarHeight(this), 0, 0); if (SystemUtils.hasNavigationBar(this)) { layStatus.setPadding(0, 0, 0, SystemUtils.getNavigationBarHeight(this)); } } getToolbar().setBackgroundColor(Color.TRANSPARENT); }
public static String getStatusMulImage(String thumbImage) { switch (AppSettings.getPictureMode()) { // MODE_AUTO case 2: if (SystemUtils.getNetworkType() == SystemUtils.NetWorkType.wifi) return thumbImage.replace("thumbnail", "bmiddle"); return thumbImage; // MODE_ALWAYS_ORIG case 1: return thumbImage.replace("thumbnail", "bmiddle"); case 3: return thumbImage.replace("thumbnail", "bmiddle"); // MODE_ALWAYS_THUMB case 0: return thumbImage; default: return thumbImage; } }
public static File getUploadFile(File source) { if (source.getName().toLowerCase().endsWith(".gif")) { Logger.w("上传图片是GIF图片,上传原图"); return source; } File file = null; String imagePath = GlobalContext.getInstance().getAppPath() + SettingUtility.getStringSetting("draft") + File.separator; int sample = 1; int maxSize = 0; int type = AppSettings.getUploadSetting(); // 自动,WIFI时原图,移动网络时高 if (type == 0) { if (SystemUtils.getNetworkType() == SystemUtils.NetWorkType.wifi) type = 1; else type = 2; } BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeFile(source.getAbsolutePath(), opts); switch (type) { // 原图 case 1: Logger.w("原图上传"); file = source; break; // 高 case 2: sample = BitmapDecoder.calculateInSampleSize(opts, 1920, 1080); Logger.w("高质量上传"); maxSize = 700 * 1024; imagePath = imagePath + "高" + File.separator + source.getName(); file = new File(imagePath); break; // 中 case 3: Logger.w("中质量上传"); sample = BitmapDecoder.calculateInSampleSize(opts, 1280, 720); maxSize = 300 * 1024; imagePath = imagePath + "中" + File.separator + source.getName(); file = new File(imagePath); break; // 低 case 4: Logger.w("低质量上传"); sample = BitmapDecoder.calculateInSampleSize(opts, 1280, 720); maxSize = 100 * 1024; imagePath = imagePath + "低" + File.separator + source.getName(); file = new File(imagePath); break; default: break; } // 压缩图片 if (type != 1 && !file.exists()) { Logger.w(String.format("压缩图片,原图片 path = %s", source.getAbsolutePath())); byte[] imageBytes = FileUtils.readFileToBytes(source); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { out.write(imageBytes); } catch (Exception e) { } Logger.w(String.format("原图片大小%sK", String.valueOf(imageBytes.length / 1024))); if (imageBytes.length > maxSize) { // 尺寸做压缩 BitmapFactory.Options options = new BitmapFactory.Options(); if (sample > 1) { options.inSampleSize = sample; Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, options); Logger.w(String.format("压缩图片至大小:%d*%d", bitmap.getWidth(), bitmap.getHeight())); out.reset(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); imageBytes = out.toByteArray(); } options.inSampleSize = 1; if (imageBytes.length > maxSize) { BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, options); Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, options); int quality = 90; out.reset(); Logger.w(String.format("压缩图片至原来的百分之%d大小", quality)); bitmap.compress(Bitmap.CompressFormat.JPEG, quality, out); while (out.toByteArray().length > maxSize) { out.reset(); quality -= 10; Logger.w(String.format("压缩图片至原来的百分之%d大小", quality)); bitmap.compress(Bitmap.CompressFormat.JPEG, quality, out); } } } try { if (!file.getParentFile().exists()) file.getParentFile().mkdirs(); Logger.w(String.format("最终图片大小%sK", String.valueOf(out.toByteArray().length / 1024))); FileOutputStream fo = new FileOutputStream(file); fo.write(out.toByteArray()); fo.flush(); fo.close(); } catch (Exception e) { e.printStackTrace(); } } return file; }