Skip to content

Instantly share code, notes, and snippets.

@kodamirmo
Last active December 19, 2015 05:49
Show Gist options
  • Save kodamirmo/5906582 to your computer and use it in GitHub Desktop.
Save kodamirmo/5906582 to your computer and use it in GitHub Desktop.
Para cortar imágenes en pedazos
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
public class ImageUtil {
private ArrayList<String> imageParts;
private String imageComplete;
private int numberOfParts;
public ImageUtil(String imagePath){
imageComplete = Base64.encodeToString(convertToByteArray(imagePath), Base64.DEFAULT);
numberOfParts=0;
imageParts=new ArrayList<String>();
cutImage();
}
private byte[] convertToByteArray(String imagePath){
Bitmap bm = BitmapFactory.decodeFile(imagePath);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
return outputStream.toByteArray();
}
private void cutImage(){
long totalSize=imageComplete.length();
int partSize=(int)totalSize/200;
int limitBottom=0;
int limitTop=partSize;
//Armamos los pedazos
for(int i=0;i<200;i++){
//Cortamos una parte
String imagePart=imageComplete.substring(limitBottom,limitTop);
imageParts.add(imagePart);
//Establecemos nuevos limites
limitBottom=limitBottom+partSize+1;
limitTop=limitTop+partSize+1;
}
//Checamos que no haya sobrado una parte
long longOfParts=200*partSize;
if(longOfParts==totalSize){
numberOfParts=20;
return;
}
else{
//Armamos el pedazo faltante
String imagePart=imageComplete.substring(limitTop+1,(int)totalSize);
imageParts.add(imagePart);
numberOfParts=21;
return;
}
}
public int getNumberOfParts(){
return numberOfParts;
}
public String getImagePart(int numberPart){
return imageParts.get(numberPart);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment