Skip to content

Commit bb524ef

Browse files
author
dongsen
committed
add bitmap p3 test
1 parent d332a85 commit bb524ef

File tree

1 file changed

+67
-5
lines changed

1 file changed

+67
-5
lines changed

app/src/main/java/com/test/devilsen/test/bitmap/P3ColorTestActivity.java

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@
22

33
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
5+
import android.graphics.Canvas;
6+
import android.graphics.ColorSpace;
7+
import android.graphics.ImageDecoder;
8+
import android.graphics.Paint;
9+
import android.os.Build;
510
import android.os.Bundle;
611
import android.util.Log;
712
import android.view.View;
813
import android.widget.ImageView;
914

1015
import androidx.annotation.Nullable;
16+
import androidx.annotation.RequiresApi;
1117
import androidx.appcompat.app.AppCompatActivity;
1218

1319
import com.test.devilsen.test.R;
1420
import com.test.devilsen.test.util.BitmapUtil;
1521

1622
import java.io.IOException;
1723
import java.io.InputStream;
24+
import java.nio.ByteBuffer;
25+
import java.nio.IntBuffer;
1826

1927

2028
public class P3ColorTestActivity extends AppCompatActivity {
@@ -34,19 +42,73 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
3442

3543
try {
3644
InputStream open = getResources().getAssets().open("image/Webkit-logo-P3.png");
37-
mBitmap = BitmapFactory.decodeStream(open);
45+
BitmapFactory.Options options = new BitmapFactory.Options();
46+
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
47+
mBitmap = BitmapFactory.decodeStream(open, null, options);
48+
// String path = "/storage/emulated/0/DCIM/P3/Webkit-logo-P3.png";
49+
// mBitmap = BitmapFactory.decodeFile(path, options);
50+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
51+
ColorSpace colorSpace = mBitmap.getColorSpace();
52+
Log.d(TAG, "bitmap color space = " + colorSpace.getName());
53+
54+
Bitmap.Config config = mBitmap.getConfig();
55+
Log.d(TAG, "bitmap config = " + config.toString());
56+
}
3857
beforeIV.setImageBitmap(mBitmap);
3958
} catch (IOException e) {
4059
e.printStackTrace();
4160
}
4261
}
4362

4463
public void change(View view) {
45-
String path = getExternalCacheDir().getAbsolutePath() + "/Webkit-logo-P3-transfer.png";
46-
boolean saveImage = BitmapUtil.saveImage(mBitmap, path);
47-
Log.d(TAG, "saved = " + saveImage + " path = " + path);
64+
int imageSize = mBitmap.getRowBytes() * mBitmap.getHeight();
65+
ByteBuffer buffer = ByteBuffer.allocateDirect(imageSize);
66+
mBitmap.copyPixelsToBuffer(buffer);
67+
buffer.position(0);
68+
69+
// 0. save directly
70+
// String path = saveBitmap(mBitmap);
71+
// Bitmap bitmap = BitmapFactory.decodeFile(path);
72+
// afterIV.setImageBitmap(bitmap);
73+
74+
int width = mBitmap.getWidth();
75+
int height = mBitmap.getHeight();
76+
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
77+
78+
// 1. canvas draw
79+
// Canvas canvas = new Canvas(bitmap);
80+
// Paint paint = new Paint();
81+
// canvas.drawBitmap(mBitmap, 0, 0, paint);
82+
83+
// 2. use pixels
84+
int[] pixels = new int[mBitmap.getWidth() * mBitmap.getHeight()];
85+
mBitmap.getPixels(pixels, 0, width, 0, 0, width, height);
86+
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
87+
88+
// 3. use ImageDecoder.Source (need api 28 android 9.0)
89+
// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
90+
// ImageDecoder.Source source = ImageDecoder.createSource(getAssets(), "image/Webkit-logo-P3.png");
91+
// try {
92+
// //使用OnHeaderDecodedListener监听解码信息
93+
// bitmap = ImageDecoder.decodeBitmap(source, (decoder, info, source1) ->
94+
// decoder.setTargetColorSpace(ColorSpace.get(ColorSpace.Named.SRGB)));
95+
// } catch (IOException e) {
96+
// e.printStackTrace();
97+
// }
98+
// }
99+
100+
// 4. use buffer(not working for P3)
101+
// bitmap.copyPixelsFromBuffer(buffer);
48102

49-
Bitmap bitmap = BitmapFactory.decodeFile(path);
50103
afterIV.setImageBitmap(bitmap);
104+
105+
saveBitmap(bitmap);
106+
}
107+
108+
private String saveBitmap(Bitmap bitmap) {
109+
String path = getExternalCacheDir().getAbsolutePath() + "/Webkit-logo-P3-transfer.png";
110+
boolean saveImage = BitmapUtil.saveImage(bitmap, path);
111+
Log.d(TAG, "saved = " + saveImage + " path = " + path);
112+
return path;
51113
}
52114
}

0 commit comments

Comments
 (0)