List of usage examples for android.graphics Bitmap compress
@WorkerThread public boolean compress(CompressFormat format, int quality, OutputStream stream)
From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.PictureObj.java
public static DbObject from(Context context, Uri imageUri) throws IOException { // Query gallery for camera picture via // Android ContentResolver interface ContentResolver cr = context.getContentResolver(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;//w w w . j a v a 2 s.c o m BitmapFactory.decodeStream(cr.openInputStream(imageUri), null, options); int targetSize = 200; int xScale = (options.outWidth + targetSize - 1) / targetSize; int yScale = (options.outHeight + targetSize - 1) / targetSize; int scale = xScale < yScale ? xScale : yScale; //uncomment this to get faster power of two scaling //for(int i = 0; i < 32; ++i) { // int mushed = scale & ~(1 << i); // if(mushed != 0) // scale = mushed; //} options.inJustDecodeBounds = false; options.inSampleSize = scale; Bitmap sourceBitmap = BitmapFactory.decodeStream(cr.openInputStream(imageUri), null, options); int width = sourceBitmap.getWidth(); int height = sourceBitmap.getHeight(); int cropSize = Math.min(width, height); float scaleSize = ((float) targetSize) / cropSize; Matrix matrix = new Matrix(); matrix.postScale(scaleSize, scaleSize); float rotation = PhotoTaker.rotationForImage(context, imageUri); if (rotation != 0f) { matrix.preRotate(rotation); } Bitmap resizedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, width, height, matrix, true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, baos); byte[] data = baos.toByteArray(); sourceBitmap.recycle(); sourceBitmap = null; resizedBitmap.recycle(); resizedBitmap = null; System.gc(); // TODO: gross. JSONObject base = new JSONObject(); if (ContentCorral.CONTENT_CORRAL_ENABLED) { try { String type = cr.getType(imageUri); if (type == null) { type = "image/jpeg"; } base.put(CorralClient.OBJ_LOCAL_URI, imageUri.toString()); base.put(CorralClient.OBJ_MIME_TYPE, type); String localIp = ContentCorral.getLocalIpAddress(); if (localIp != null) { base.put(Contact.ATTR_LAN_IP, localIp); } } catch (JSONException e) { Log.e(TAG, "impossible json error possible!"); } } return from(base, data); }
From source file:Main.java
public static String savePicture(Bitmap bmp, String file) { File filepicture = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); if (file == null || file.equals("")) { file = filepicture + "/" + System.currentTimeMillis() + ".jpg"; }/*from ww w. ja va 2s. co m*/ FileOutputStream outputStream = null; try { outputStream = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); if (outputStream != null) { try { outputStream.flush(); outputStream.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return ""; } } finally { if (outputStream != null) { try { outputStream.flush(); outputStream.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } return "file://" + file; }
From source file:Main.java
public static byte[] bmpToByteArray2(final Bitmap bmp, final boolean needRecycle) { int i;/* ww w .j a v a2 s. c om*/ int j; if (bmp.getHeight() > bmp.getWidth()) { i = bmp.getWidth(); j = bmp.getWidth(); } else { i = bmp.getHeight(); j = bmp.getHeight(); } Bitmap localBitmap = Bitmap.createBitmap(i, j, Bitmap.Config.RGB_565); Canvas localCanvas = new Canvas(localBitmap); while (true) { localCanvas.drawBitmap(bmp, new Rect(0, 0, i, j), new Rect(0, 0, i, j), null); if (needRecycle) bmp.recycle(); ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream(); localBitmap.compress(Bitmap.CompressFormat.JPEG, 100, localByteArrayOutputStream); localBitmap.recycle(); byte[] arrayOfByte = localByteArrayOutputStream.toByteArray(); try { localByteArrayOutputStream.close(); return arrayOfByte; } catch (Exception e) { //F.out(e); } i = bmp.getHeight(); j = bmp.getHeight(); } }
From source file:Main.java
public static void saveImageToSD(Context ctx, String filePath, Bitmap bitmap, int quality) throws IOException { if (bitmap != null) { File file = new File(filePath); File dir = new File(filePath.substring(0, filePath.lastIndexOf(File.separator))); if (!dir.exists()) { dir.mkdirs();/* w w w . ja v a 2s .co m*/ } file.createNewFile(); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos); bos.flush(); bos.close(); } }
From source file:Main.java
public static Bitmap decodeSampledBitmapFromFd(String filePath) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;//from ww w .jav a 2s .c o m BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize // if(Build.VERSION.SDK_INT < 19) // { // options.inSampleSize = calculateInSampleSize(options, 300, 300); // } // else // { options.inSampleSize = calculateInSampleSize(options, 480, 480); // } // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; Bitmap bm = BitmapFactory.decodeFile(filePath, options); if (bm == null) { return null; } int degree = readPictureDegree(filePath); bm = rotateBitmap(bm, degree); ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 30, baos); } finally { try { if (baos != null) baos.close(); } catch (IOException e) { e.printStackTrace(); } } return bm; }
From source file:Main.java
/** * Writes file to external storage and returns the filename. * Writes as a JPEG file./* ww w. j a v a2 s. c o m*/ * If you want to convert to a URI you need to prepend file:// * @param b * @param fileName * @return */ static public String saveImageToExternal(Bitmap b, String fileName) { //}, int width, int height) { FileOutputStream fos; String mypath = Environment.getExternalStorageDirectory() + "/" + fileName + ".jpg"; File file = new File(mypath); try { file.createNewFile(); fos = new FileOutputStream(file); // Use the compress method on the BitMap object to write image to the OutputStream b.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } ; return file.toString(); }
From source file:jog.my.memory.gcm.ServerUtilities.java
/** * Uploads a file to the server.//from w w w. j a v a 2 s. co m * @param context - Context of the app where the file is * @param serverUrl - URL of the server * @param uri - URI of the file * @return - response of posting the file to server */ public static String postFile(Context context, String serverUrl, Uri uri) { //TODO: Get the location sent up in here too! HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(serverUrl + "/upload"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); // //Create the FileBody // final File file = new File(uri.getPath()); // FileBody fb = new FileBody(file); // deal with the file ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); Bitmap bitmap = BitmapFactory.decodeFile(getRealPathFromURI(uri, context)); bitmap.compress(Bitmap.CompressFormat.JPEG, 75, byteArrayOutputStream); byte[] byteData = byteArrayOutputStream.toByteArray(); //String strData = Base64.encodeToString(data, Base64.DEFAULT); // I have no idea why Im doing this ByteArrayBody byteArrayBody = new ByteArrayBody(byteData, "image"); // second parameter is the name of the image (//TODO HOW DO I MAKE IT USE THE IMAGE FILENAME?) builder.addPart("myFile", byteArrayBody); builder.addTextBody("foo", "test text"); HttpEntity entity = builder.build(); post.setEntity(entity); try { HttpResponse response = client.execute(post); Log.d(TAG, "The response code was: " + response.getStatusLine().getStatusCode()); } catch (Exception e) { Log.d(TAG, "The image was not successfully uploaded."); } return null; // HttpClient httpclient = new DefaultHttpClient(); // HttpPost httppost = new HttpPost(serverUrl+"/postFile.do"); // // InputStream stream = null; // try { // stream = context.getContentResolver().openInputStream(uri); // // InputStreamEntity reqEntity = new InputStreamEntity(stream, -1); // // httppost.setEntity(reqEntity); // // HttpResponse response = httpclient.execute(httppost); // Log.d(TAG,"The response code was: "+response.getStatusLine().getStatusCode()); // if (response.getStatusLine().getStatusCode() == 200) { // // file uploaded successfully! // } else { // throw new RuntimeException("server couldn't handle request"); // } // return response.toString(); // } catch (Exception e) { // e.printStackTrace(); // // // handle error // } finally { // try { // stream.close(); // }catch(IOException ioe){ // ioe.printStackTrace(); // } // } // return null; }
From source file:jog.my.memory.gcm.ServerUtilities.java
/** * Convert images to the string representation * @param pics - pictures to send/* w ww.ja v a 2s .com*/ * @return arraylist of string representation of pictures */ public static ArrayList<String> convertPhotosToStringRepresentation(ArrayList<Picture> pics) { ArrayList<String> listPhotos = new ArrayList<>(); for (int i = 0; i < pics.size(); i++) { // String stringEncodedImage = // Base64.encodeToString(pics.get(i).getmImageAsByteArray(), Base64.DEFAULT); // listPhotos.add(stringEncodedImage); Bitmap bitmap = pics.get(i).getmImage(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); byte[] bitmapByte = outputStream.toByteArray(); listPhotos.add(Base64.encodeToString(bitmapByte, Base64.DEFAULT)); // try { // listPhotos.add(new String(bitmapByte)); // } // catch(Exception e){ // Log.d(TAG,"The encoding didn't work"); // } } return listPhotos; }
From source file:com.dv.Utils.Tools.java
/** * byte[]/*from w w w.j a v a 2 s . c o m*/ * * @param bitmap * @return */ public static byte[] bitmap2byte(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); return baos.toByteArray(); }
From source file:Main.java
/** * save the bitmap to local,add give a white bg color * @param bitmap// w w w . j a va2 s. com * @param path * @return */ public static boolean saveBitmapNoBgToSdCard(Bitmap bitmap, String path) { BufferedOutputStream bos = null; try { File file = new File(path); if (file.exists()) file.delete(); bos = new BufferedOutputStream(new FileOutputStream(file)); int w = bitmap.getWidth(); int h = bitmap.getHeight(); int w_new = w; int h_new = h; Bitmap resultBitmap = Bitmap.createBitmap(w_new, h_new, Bitmap.Config.ARGB_8888); // Paint paint = new Paint(); // paint.setColor(Color.WHITE); Canvas canvas = new Canvas(resultBitmap); canvas.drawColor(Color.WHITE); canvas.drawBitmap(bitmap, new Rect(0, 0, w, h), new Rect(0, 0, w_new, h_new), null); resultBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos); bos.flush(); resultBitmap.recycle(); return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; }