我有一个这样的方法,它只是将图像分成几个块
public void splitImage(Bitmap bmp, int chunks, int imageCode) {
int chunkHeight, chunkWidth;
int rows, cols;
rows = cols = 5;
//To store all the small image chunks in bitmap format in this list
ArrayList<Bitmap> greyImageChunks = new ArrayList<Bitmap>(chunks);
ArrayList<Bitmap> greenImageChunks = new ArrayList<Bitmap>(chunks);
ArrayList<Bitmap> redImageChunks = new ArrayList<Bitmap>(chunks);
//Getting the scaled bitmap of the source image
//BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
//Bitmap bitmap = drawable.getBitmap();
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmp, bmp.getWidth(), bmp.getHeight(), true);
rows = cols = (int) Math.sqrt(chunks);
chunkHeight = bmp.getHeight()/rows;
chunkWidth = bmp.getWidth()/cols;
//xCoord and yCoord are the pixel positions of the image chunks
int yCoord = 0;
for(int x=0; x<rows; x++){
int xCoord = 0;
for(int y=0; y<cols; y++){
if(imageCode == 1) {
greyImageChunks.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
} else if(imageCode == 2) {
greenImageChunks.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
} else if(imageCode == 3) {
redImageChunks.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
}
xCoord += chunkWidth;
}
yCoord += chunkHeight;
}
}
这里我想使用另一种方法(例如mergeImages())从greyImageChunks,greenImageChunks和redImageChunks中检索块。
外部世界无法访问它们,因为它们是本地创建的。 是否有任何方法可以访问此方法之外的这些数组列表。 可以从一个本地ArrayList到另一个全局ArrayList的任何复制方法吗? 请帮帮我。
注意:ArrayList大小应该是动态的,因为ArrayList的大小是使用splitImage()方法中的int块定义的
答案 0 :(得分:2)
如果我理解正确,请创建一个全局ArrayList
说mArrayList
。
ArrayList<Bitmap> mArrayList= new ArrayList<Bitmap>();
现在在你的方法中,在for循环之后将所有三个本地ArrayLists添加到Global ArrayList。
for(int x=0; x<rows; x++){
int xCoord = 0;
for(int y=0; y<cols; y++){
if(imageCode == 1) {
greyImageChunks.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
} else if(imageCode == 2) {
greenImageChunks.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
} else if(imageCode == 3) {
redImageChunks.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
}
xCoord += chunkWidth;
}
yCoord += chunkHeight;
}
mArrayList.addAll(greyImageChunks);
mArrayList.addAll(greenImageChunks);
mArrayList.addAll(redImageChunks);
答案 1 :(得分:1)
对imagechunks使用数组而不是ArrayList,并让你的splitImage()
方法返回位图数组的Arraylist?
像这样:
return ArrayList<Bitmap[]> chunksArray;
编辑:
public ArrayList<Bitmap[]> splitImage(Bitmap bmp, int chunks, int imageCode){
int chunkHeight, chunkWidth;
int rows, cols;
rows = cols = 5;
//To store all the small image chunks in bitmap format in this list
// ArrayList<Bitmap> greyImageChunks = new ArrayList<Bitmap>(chunks);
//ArrayList<Bitmap> greenImageChunks = new ArrayList<Bitmap>(chunks);
// ArrayList<Bitmap> redImageChunks = new ArrayList<Bitmap>(chunks);
Bitmap [] greyImageChunks = new Bitmap[chunks];
Bitmap [] greenImageChunks = new Bitmap[chunks];
Bitmap [] redImageChunks = new Bitmap[chunks];
ArrayList< Bitmap[]> arrayList = new ArrayList<>();
//Getting the scaled bitmap of the source image
//BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
//Bitmap bitmap = drawable.getBitmap();
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmp, bmp.getWidth(), bmp.getHeight(), true);
rows = cols = (int) Math.sqrt(chunks);
chunkHeight = bmp.getHeight()/rows;
chunkWidth = bmp.getWidth()/cols;
//xCoord and yCoord are the pixel positions of the image chunks
int yCoord = 0, greyCount=0, greenCount=0, redCount=0;
for(int x=0; x<rows; x++){
int xCoord = 0;
for(int y=0; y<cols; y++){
if(imageCode == 1) {
greyImageChunks[greyCount]= Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight);
greyCount++;
} else if(imageCode == 2) {
greenImageChunks[greenCount]= Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight);
greenCount++;
} else if(imageCode == 3) {
redImageChunks[redCount]= Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight);
redCount++;
}
xCoord += chunkWidth;
}
yCoord += chunkHeight;
}
arrayList.add(greyImageChunks);
arrayList.add(greenImageChunks);
arrayList.add(redImageChunks);
return arrayList;
}