[编辑:我做了一个最小的项目,试图缩小正在发生的事情。底部的代码在保存时仍会生成相同的工件]
我有一个使用Paths绘制简单2D几何的应用程序。形状都是纯色,有时是alpha< 255,可以用线条装饰。在绘制几何图形的视图中,从未出现过绘制事物的问题。但是,当我使用相同的代码绘制到位图,然后将其保存为JPEG(100质量)或PNG时,输出文件的纯色区域中始终存在相同的伪像。这是一种通常与JPEG压缩相关的斑点。
我试过以下
所有产生相同的文物,既不会使情况变得更糟也不会更好。
public class MainActivity extends AppCompatActivity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = getApplicationContext();
}
// button OnClick listener
public void saveImage(View view) {
new saveBitmapToDisk().execute(false);
}
public Bitmap getBitmap() {
final int bitmapHeight = 600, bitmapWidth = 600;
Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
Canvas bitmapCanvas = new Canvas(bitmap);
float[] triangle = new float[6];
triangle[0] = bitmapWidth / 2;
triangle[1] = 0;
triangle[2] = 0;
triangle[3] = bitmapHeight / 2;
triangle[4] = bitmapWidth / 2;
triangle[5] = bitmapHeight / 2;
Path solidPath = new Path();
Paint solidPaint = new Paint();
solidPaint.setStyle(Paint.Style.FILL);
solidPath.moveTo(triangle[0], triangle[1]);
for(int i = 2; i < triangle.length; i += 2)
solidPath.lineTo(triangle[i], triangle[i+1]);
solidPath.close();
solidPaint.setColor(Color.GREEN);
bitmapCanvas.drawPath(solidPath, solidPaint);
return bitmap;
}
private class saveBitmapToDisk extends AsyncTask<Boolean, Integer, Uri> {
Boolean toShare;
@Override
protected Uri doInBackground(Boolean... shareFile) {
this.toShare = shareFile[0];
final String appName = context.getResources().getString(R.string.app_name);
final String IMAGE_SAVE_DIRECTORY = String.format("/%s/", appName);
final String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + IMAGE_SAVE_DIRECTORY;
File dir, file;
try {
dir = new File(fullPath);
if (!dir.exists())
dir.mkdirs();
OutputStream fOut;
file = new File(fullPath, String.format("%s.png", appName));
for (int suffix = 0; file.exists(); suffix++)
file = new File(fullPath, String.format("%s%03d.png", appName, suffix));
file.createNewFile();
fOut = new FileOutputStream(file);
Bitmap saveBitmap = getBitmap();
saveBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
} catch (OutOfMemoryError e) {
Log.e("MainActivity", "Out of Memory saving bitmap; bitmap is too large");
return null;
} catch (Exception e) {
Log.e("MainActivity", e.getMessage());
return null;
}
return Uri.fromFile(file);
}
@Override
protected void onPostExecute(Uri uri) {
super.onPostExecute(uri);
Toast.makeText(context, "Image saved", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:3)
编辑: 这条线
MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
导致转换为jpeg。
保存图像的正确方法是
ContentValues values = new ContentValues();
values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/png");
values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
这是我直接发送生成文件的简化测试程序
public class Test2Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new saveBitmapToDisk().execute();
}
public Bitmap getBitmap() {
final int bitmapHeight = 600, bitmapWidth = 600;
Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
Canvas bitmapCanvas = new Canvas(bitmap);
Paint solidPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
solidPaint.setStyle(Paint.Style.FILL);
solidPaint.setColor(Color.RED);
bitmapCanvas.drawCircle(300, 300, 200, solidPaint);
return bitmap;
}
private class saveBitmapToDisk extends AsyncTask<Void, Void, Uri> {
Boolean toShare;
@Override
protected Uri doInBackground(Void... shareFile) {
Context context = Test2Activity.this;
try {
File file = new File(context.getExternalFilesDir(null), "test.png");
FileOutputStream fOut = new FileOutputStream(file);
Bitmap saveBitmap = getBitmap();
saveBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
return Uri.fromFile(file);
} catch (OutOfMemoryError e) {
Log.e("MainActivity", "Out of Memory saving bitmap; bitmap is too large");
return null;
} catch (Exception e) {
Log.e("MainActivity", e.getMessage());
return null;
}
}
@Override
protected void onPostExecute(Uri uri) {
Context context = Test2Activity.this;
Toast.makeText(context, "Image saved", Toast.LENGTH_SHORT).show();
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
Test2Activity.this.startActivity(intent);
}
}
}
答案 1 :(得分:1)
像这样的伪像是JPEG压缩的自然和不可避免的后果。
它们不应该在PNG压缩中出现。如果您在创建PNG文件时遇到此类工件,我会打赌您根本不创建PNG流,而是在具有PNG扩展名的文件中创建JPEG流。没有合适的解码器依赖于文件扩展名。
答案 2 :(得分:0)
我注意到代码中有两件事:
1)您保存的文件名为String.format("%s.jpg", appName)
或String.format("%s%03d.png", appName, suffix)
,与实际编码无关。
2)您保存的位图的密度由prefs.saveImageDensity().get()
决定,因此它可能与您在屏幕上看到的位图的实际密度不同。
也许你把自己弄糊涂了1)或者2)导致你看到的压缩文物?