Java 位图“;“图像”;传递到另一个活动(内存不足)
请先阅读完整问题,然后再将其标记为重复或否决 我正在开发一个应用程序,它可以对图片进行切片,运行google vision来识别每个图片块或切片中的文本,并运行OCR来检测图片块中是否填充了圆圈气泡。但是,当我在数组中切片位图图像并将其传递给其他活动进行处理时,它会因过度使用内存而崩溃。我知道我可以压缩它,但我已经尝试过了(虽然我不想压缩它,因为我需要运行google vision,可能无法准确地提取文本),但它不起作用,因为有46个图像切片。如果不上传到云上,我怎么做呢?因为这可能需要很长时间,所以我要重新获取它以进行处理。任何替代方案都是非常受欢迎的。我在这个问题上纠缠了好一阵子Java 位图“;“图像”;传递到另一个活动(内存不足),java,android,image-processing,bit-manipulation,android-bitmap,Java,Android,Image Processing,Bit Manipulation,Android Bitmap,请先阅读完整问题,然后再将其标记为重复或否决 我正在开发一个应用程序,它可以对图片进行切片,运行google vision来识别每个图片块或切片中的文本,并运行OCR来检测图片块中是否填充了圆圈气泡。但是,当我在数组中切片位图图像并将其传递给其他活动进行处理时,它会因过度使用内存而崩溃。我知道我可以压缩它,但我已经尝试过了(虽然我不想压缩它,因为我需要运行google vision,可能无法准确地提取文本),但它不起作用,因为有46个图像切片。如果不上传到云上,我怎么做呢?因为这可能需要很长时间
import android.content.Intent;.....
public class ProcessesdResult extends AppCompatActivity {
TextView tvProcessedText;
Button btnImageSlice;
Bitmap image;
int chunkNumbers =46;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_processesd_result);
Intent intenttakeattendance = getIntent();
String fname = intenttakeattendance.getStringExtra("fname");
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root);
String photoPath = myDir+"/sams_images/"+ fname;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
image = BitmapFactory.decodeFile(photoPath, options);
btnImageSlice=findViewById(R.id.btnimageslice);
btnImageSlice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
splitImage(image, chunkNumbers) ;
}
});
}
private void splitImage(Bitmap image, int chunkNumbers) {
//For the number of rows and columns of the grid to be displayed
int rows =23;
int cols =2;
//For height and width of the small image chunks
int chunkHeight,chunkWidth;
//To store all the small image chunks in bitmap format in this list
ArrayList<Bitmap> chunkedImages = new ArrayList<Bitmap>(chunkNumbers);
//Getting the scaled bitmap of the source image
Bitmap scaledBitmap = Bitmap.createScaledBitmap(image, image.getWidth(), image.getHeight(), true);
chunkHeight = image.getHeight()/rows;
chunkWidth = image.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++){
chunkedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
xCoord += chunkWidth;
}
yCoord += chunkHeight;
}
//Start a new activity to show these chunks into a grid
Intent intent = new Intent(ProcessesdResult.this, ChunkedImageActivity.class);
intent.putParcelableArrayListExtra("image chunks", chunkedImages);
startActivity(intent);
}
}
导入android.content.Intent;。。。。。
公共类ProcessesdResult扩展了AppCompatActivity{
文本视图tvProcessedText;
按钮btnImageSlice;
位图图像;
整数=46;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u processs\u result);
Intent intenttakeAttention=getIntent();
String fname=intenttakeAttention.getStringExtra(“fname”);
String root=Environment.getExternalStorageDirectory().toString();
文件myDir=新文件(根);
字符串photoPath=myDir+“/sams_images/”+fname;
BitmapFactory.Options=new-BitmapFactory.Options();
options.inPreferredConfig=Bitmap.Config.ARGB_8888;
image=BitmapFactory.decodeFile(光路,选项);
btnImageSlice=findviewbyd(R.id.btnImageSlice);
btnImageSlice.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
拆分图像(图像、区块编号);
}
});
}
私有void splitImage(位图图像,int chunknumber){
//用于显示网格的行数和列数
int行=23;
int cols=2;
//用于小图像块的高度和宽度
int chunkHeight,chunkWidth;
//要在此列表中以位图格式存储所有小图像块
ArrayList chunkeImage=新的ArrayList(chunkNumber);
//获取源图像的缩放位图
位图缩放位图=位图.createScaledBitmap(image,image.getWidth(),image.getHeight(),true);
chunkHeight=image.getHeight()/行;
chunkWidth=image.getWidth()/cols;
//xCoord和yCoord是图像块的像素位置
int yCoord=0;
对于(int x=0;x您不希望在活动之间传递对象,尤其是像位图这样的大型对象。我建议您将位图保存在设备文件系统中,然后传递URI列表。这样保存位图,并在使用完位图后循环使用它们,还可以减少您在循环过程中使用的RAM将图像切片
要将位图保存为文件,请参考以下问题:
所以基本上你的循环应该是这样的:
for(int x=0; x<rows; x++){
int xCoord = 0;
for(int y=0; y<cols; y++){
Bitmap image = Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight);
Uri uri = saveBitmapAsFile(image);
image.recycle();
xCoord += chunkWidth;
}
yCoord += chunkHeight;
}
for(int x=0;x在清单中使用android:largeHeap=“true”如果仍然出现相同的错误,请尝试以下操作:
而不是发送“intent.putParcelableArrayListExtra(“图像块”,chunkedImages)
位图到另一个活动,将该图像保存到本地存储,并在任何需要的地方使用path。我建议使用static创建另一个数据(位图)存储类。
使用此类保存位图并调用另一个活动进行读取
链接很有用