当我尝试更改像素颜色时如何修复 Android 上的 IllegalStateException
How to fix IlegalStateException on Android when I try to change a pixel color
我试图使用以下代码将白色像素更改为透明像素。
private void transformPixelWhiteToTransparent() {
MyLog.i(this, "removePixelBrancoDeImagem iniciado");
String nomeArquivo = "RESMARFOC0001.jpg";
File file = new File( "sdcard/Imagem/"+nomeArquivo);
String path = file.getAbsolutePath();
MyLog.i(this, "path: "+path);
if(file.exists()) {
try{
MyLog.i(this, "file exists");
Bitmap bm = BitmapFactory.decodeFile(path);
MyLog.i(this, "cheguei fim");
for(int x = 0; x<bm.getWidth(); x++){
for(int y = 0; y<bm.getHeight(); y++){
if(bm.getPixel(x, y) == Color.WHITE){
bm.setPixel(x, y, Color.TRANSPARENT);
}
}
}
MyLog.i(this, "fim salvar arquivo");
}catch (Exception e){
MyLog.i(this, "erro: "+e);
}
}
}
但我在下一行中收到错误 IlegalStateException
"bm.setPixel(x, y, Color.TRANSPARENT);"
我该如何解决?
我找到了让我的代码工作的方法。
private void transformPixelWhiteToTransparent() {
String nomeArquivo = "RESMARFOC0001.jpg"; // crie uma marca e depois troque o nome dela aqui
File file = new File( "sdcard/Imagem/"+nomeArquivo);
String path = file.getAbsolutePath();
if(file.exists()) { // se arquivo existe carrega para upload
try{
Bitmap bm1 = BitmapFactory.decodeFile(path);
bm1.setHasAlpha(true);
Bitmap bmMod = bm1.copy(bm1.getConfig(),true);
for(int x = 0; x<bmMod.getWidth(); x++){
for(int y = 0; y<bmMod.getHeight(); y++){
if(bmMod.getPixel(x, y) == Color.WHITE){
bmMod.setPixel(x, y, Color.TRANSPARENT); // Color.BLUE
}
}
}
// more code to save the bitmap as PNG file
}catch (Exception e){
MyLog.i(this, "erro: "+e);
}
}
}
与我以前的代码唯一不同的是行
Bitmap bmMod = bm1.copy(bm1.getConfig(),true);
我需要创建原始位图的副本才能修改复制的位图。
我试图使用以下代码将白色像素更改为透明像素。
private void transformPixelWhiteToTransparent() {
MyLog.i(this, "removePixelBrancoDeImagem iniciado");
String nomeArquivo = "RESMARFOC0001.jpg";
File file = new File( "sdcard/Imagem/"+nomeArquivo);
String path = file.getAbsolutePath();
MyLog.i(this, "path: "+path);
if(file.exists()) {
try{
MyLog.i(this, "file exists");
Bitmap bm = BitmapFactory.decodeFile(path);
MyLog.i(this, "cheguei fim");
for(int x = 0; x<bm.getWidth(); x++){
for(int y = 0; y<bm.getHeight(); y++){
if(bm.getPixel(x, y) == Color.WHITE){
bm.setPixel(x, y, Color.TRANSPARENT);
}
}
}
MyLog.i(this, "fim salvar arquivo");
}catch (Exception e){
MyLog.i(this, "erro: "+e);
}
}
}
但我在下一行中收到错误 IlegalStateException
"bm.setPixel(x, y, Color.TRANSPARENT);"
我该如何解决?
我找到了让我的代码工作的方法。
private void transformPixelWhiteToTransparent() {
String nomeArquivo = "RESMARFOC0001.jpg"; // crie uma marca e depois troque o nome dela aqui
File file = new File( "sdcard/Imagem/"+nomeArquivo);
String path = file.getAbsolutePath();
if(file.exists()) { // se arquivo existe carrega para upload
try{
Bitmap bm1 = BitmapFactory.decodeFile(path);
bm1.setHasAlpha(true);
Bitmap bmMod = bm1.copy(bm1.getConfig(),true);
for(int x = 0; x<bmMod.getWidth(); x++){
for(int y = 0; y<bmMod.getHeight(); y++){
if(bmMod.getPixel(x, y) == Color.WHITE){
bmMod.setPixel(x, y, Color.TRANSPARENT); // Color.BLUE
}
}
}
// more code to save the bitmap as PNG file
}catch (Exception e){
MyLog.i(this, "erro: "+e);
}
}
}
与我以前的代码唯一不同的是行
Bitmap bmMod = bm1.copy(bm1.getConfig(),true);
我需要创建原始位图的副本才能修改复制的位图。