gpt4 book ai didi

android - 位图压缩给出空文件(0 字节)

转载 作者:行者123 更新时间:2023-11-30 02:45:21 26 4
gpt4 key购买 nike

编辑 - 另一个更新!

有趣的是,我在我的 nexus 5 上尝试了相同的代码,图像预览是正确的,但图像本身只是黑色。

正在使用的设备是s3 minis,在这些设备上无法打开图像并且不显示预览。

我的目标是 sdk 版本 16。

此外,我将我的 nexus 插回我的开发机器并作为存储设备打开,图像未显示。

然后我将手机中的图像通过电子邮件发送给自己,该死的东西显示正常,这让我更加困惑,我唯一能想到的是图像太大无法显示?

//////////////////////////////////////////////////////////////////////////////////////

我正在尝试创建一个图像,然后将其保存到文件中,它在屏幕上显示正常并且文件已创建,但是该文件无法打开并且大小为 0 字节。以前有人遇到过这个问题吗?这是我的代码:

private class SampleView extends View {


public SampleView(Context context) {
super(context);
setFocusable(true);

}
@Override
protected void onDraw(Canvas canvas) {

EANMaker e = new EANMaker("123456789963");
Paint paint = new Paint();
View view = this;
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(view.DRAWING_CACHE_QUALITY_HIGH);

canvas.drawColor(Color.WHITE);


Bitmap b = Bitmap.createBitmap(500, 500, Bitmap.Config.ALPHA_8);
Canvas c = new Canvas(b);
c.drawRect(0, 0, 500, 500, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
paint.setTextSize(40);
paint.setTextScaleX(1.f);
paint.setAlpha(0);
paint.setAntiAlias(true);
c.drawText("Carton ID", 60, 40, paint);
paint.setTextSize(160);
Typeface tf = gettypeFace(MyActivity.this, "ean.ttf");
paint.setTypeface(tf);
c.drawText(e.getCode(), 60, 180, paint);
paint.setColor(Color.WHITE);
canvas.drawBitmap(b, 50,50, paint);

FileOutputStream fos = null;
try {
fos = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/barcode.png");
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
fos = null;
} catch (IOException ee) {
ee.printStackTrace();
} catch (Exception p)
{
p.printStackTrace();
}
finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ee) {
ee.printStackTrace();
}
}
}
}

}

public Typeface gettypeFace(Context c, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t = Typeface.createFromAsset(c.getAssets(),
assetPath);
cache.put(assetPath, t);
} catch (Exception e) {
Log.e("TAG", "Could not get typeface '" + assetPath
+ "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}

编辑,我一直在玩这个,现在可以得到一个 6k 文件,但是仍然无法打开,我怀疑其中一个 Canvas 没有被绘制到位图中,但我完全不知道是什么还有事要做,我最新的代码如下

    private class SampleView extends View {


public SampleView(Context context) {
super(context);
setFocusable(true);

}
@Override
protected void onDraw(Canvas canvas) {


EANMaker e = new EANMaker("123456789963");
Paint paint = new Paint();
canvas.drawColor(Color.WHITE);
Bitmap b = Bitmap.createBitmap(900, 480, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
canvas.drawBitmap(b,0,0, paint);

//paint.setTextScaleX(1.f);

paint.setColor(Color.BLACK);
paint.setTextSize(40);
c.drawText("Carton LABEL", 20, 40, paint);
c.drawText("Carton ID = 1 Packed By = Test User", 20, 80, paint);
c.drawText("Despatch Date = 05/08/2014", 20, 120, paint);
paint.setTextSize(160);
Typeface tf = gettypeFace(MyActivity.this, "ean.ttf");
paint.setTypeface(tf);
c.drawText(e.getCode(), 60, 280, paint);
paint.setColor(Color.WHITE);


FileOutputStream fos = null;
try {
fos = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/barcode.png");
b.compress(Bitmap.CompressFormat.PNG, 80, fos);
fos.flush();
fos.close();
fos = null;
} catch (IOException ee) {
ee.printStackTrace();
} catch (Exception p)
{
p.printStackTrace();
}
finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ee) {
ee.printStackTrace();
}
}
}

}

}

public Typeface gettypeFace(Context c, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t = Typeface.createFromAsset(c.getAssets(),
assetPath);
cache.put(assetPath, t);
} catch (Exception e) {
Log.e("TAG", "Could not get typeface '" + assetPath
+ "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}

和我的 logcat 来证明没有错误:

08-05 13:13:02.371  26627-26627/com.example.imagetest I/System.out﹕ debugger has settled (1443)
08-05 13:13:03.082 26627-26627/com.example.imagetest D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
08-05 13:13:03.082 26627-26627/com.example.imagetest D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
08-05 13:13:03.092 26627-26627/com.example.imagetest D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
08-05 13:13:03.112 26627-26627/com.example.imagetest D/OpenGLRenderer﹕ Enabling debug mode 0
08-05 13:13:03.162 26627-26627/com.example.imagetest I/System.out﹕ Full code: 1234567899633
08-05 13:13:03.162 26627-26627/com.example.imagetest I/System.out﹕ Generated code: $!23E5GH-ijjgdd!
08-05 13:13:03.192 26627-26627/com.example.imagetest D/dalvikvm﹕ GC_FOR_ALLOC freed 84K, 13% free 9464K/10823K, paused 22ms, total 32ms
08-05 13:13:03.192 26627-26627/com.example.imagetest I/dalvikvm-heap﹕ Grow heap (frag case) to 12.093MB for 1728016-byte allocation
08-05 13:13:03.222 26627-26629/com.example.imagetest D/dalvikvm﹕ GC_CONCURRENT freed 39K, 12% free 11112K/12551K, paused 13ms+3ms, total 31ms
08-05 13:13:03.222 26627-26627/com.example.imagetest D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 9ms

最佳答案

正在创建图像,它们太大而无法在设备上正确打开。

关于android - 位图压缩给出空文件(0 字节),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25134156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com