Android 图像处理 - Bitmap 图像处理观察记录(基本图像复制、带目录创建的图像复制、字节流处理的图像复制、并发图像复制、单线程池顺序图像复制)

Bitmap 图像处理观察记录

1、基本图像复制
  1. 从应用内部存储目录读取 test.png

  2. 使用 BitmapFactory 解码为 Bitmap 对象

  3. 将 Bitmap 重新压缩保存为 newTest.png

  4. 操作成功,compress 返回 true

File file = new File(getFilesDir(), "test.png");
String absolutePath = file.getAbsolutePath();

Bitmap bitmap = BitmapFactory.decodeFile(absolutePath);

File newFile = new File(getFilesDir(), "newTest.png");
try (FileOutputStream fileOutputStream = new FileOutputStream(newFile)) {

    boolean compress = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
    Log.i(TAG, "compress: " + compress);
} catch (IOException e) {
    e.printStackTrace();
}
# 输出结果

compress: true
2、带目录创建的图像复制
  1. 从应用内部存储目录读取 test.png

  2. 使用 BitmapFactory 解码为 Bitmap 对象

  3. 先在内部存储创建 test 目录,然后将新图像保存在这个目录下

  4. 将 Bitmap 重新压缩保存为 newTest.png

  5. 操作成功,compress 返回 true

File file = new File(getFilesDir(), "test.png");
String absolutePath = file.getAbsolutePath();

Bitmap bitmap = BitmapFactory.decodeFile(absolutePath);

File dir = new File(getFilesDir(), "test");
if (!dir.exists()) dir.mkdirs();

File newFile = new File(dir, "newTest.png");
try (FileOutputStream fileOutputStream = new FileOutputStream(newFile)) {

    boolean compress = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
    Log.i(TAG, "compress: " + compress);
} catch (IOException e) {
    e.printStackTrace();
}
# 输出结果

compress: true
3、字节流处理的图像复制
  1. 使用 FileInputStream 和 ByteArrayOutputStream 从应用内部存储目录读取 test.png

  2. 使用 BitmapFactory 解码为 Bitmap 对象

  3. 将 Bitmap 重新压缩保存为 newTest.png

  4. 操作成功,compress 返回 true

File file = new File(getFilesDir(), "test.png");
try {
    FileInputStream fileInputStream = new FileInputStream(file);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len;
    while ((len = fileInputStream.read(buffer)) != -1) {
        byteArrayOutputStream.write(buffer, 0, len);
    }
    byte[] bytes = byteArrayOutputStream.toByteArray();
    Log.i(TAG, "bytes length: " + bytes.length);

    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

    File newFile = new File(getFilesDir(), "newTest.png");
    FileOutputStream fileOutputStream = new FileOutputStream(newFile);

    boolean compress = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
    Log.i(TAG, "compress: " + compress);
} catch (IOException e) {
    e.printStackTrace();
}
# 输出结果

bytes length: 28270
compress: true
4、并发图像复制
  1. 同时启动 10 个线程并行处理

  2. 每个线程独立完成读取、解码和保存操作

  3. 所有线程都成功完成任务

  4. 输出结果显示处理顺序不确定,即非顺序完成

private void test() {
    for (int i = 0; i < 10; i++) {
        int finalI = i;
        new Thread(() -> {
            test4_1_task("newStaff" + (finalI + 1) + ".jpg");
        }).start();
    }
}
private void test_task(String newFileName) {
    File file = new File(getFilesDir(), "staff.jpg");
    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fileInputStream.read(buffer)) != -1) {
            byteArrayOutputStream.write(buffer, 0, len);
        }
        byte[] bytes = byteArrayOutputStream.toByteArray();
        Log.i(TAG, "bytes length: " + bytes.length);

        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

        File newFile = new File(getFilesDir(), newFileName);
        FileOutputStream fileOutputStream = new FileOutputStream(newFile);

        boolean compress = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
        Log.i(TAG, newFileName + " compress: " + compress);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
# 输出结果

bytes length: 31530
bytes length: 31530
bytes length: 31530
bytes length: 31530
bytes length: 31530
bytes length: 31530
bytes length: 31530
bytes length: 31530
bytes length: 31530
bytes length: 31530
newStaff2.jpg compress: true
newStaff1.jpg compress: true
newStaff3.jpg compress: true
newStaff6.jpg compress: true
newStaff4.jpg compress: true
newStaff5.jpg compress: true
newStaff8.jpg compress: true
newStaff9.jpg compress: true
newStaff10.jpg compress: true
newStaff7.jpg compress: true
5、单线程池顺序图像复制
  1. 使用 Executors 创建单线程池

  2. 通过递归调用实现顺序任务执行

  3. 每个任务完成后才处理下一个

  4. 输出严格按顺序完成

private ExecutorService pool;
private int index = 0;
private int indexTotal = 10;
private void test() {
    pool = Executors.newSingleThreadExecutor();
    test4_2_task();
}
private void test_task() {
    pool.execute(() -> {
        index++;
        if (index > indexTotal) {
            pool.shutdown();
            return;
        }

        File file = new File(getFilesDir(), "staff.jpg");
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, len);
            }
            byte[] bytes = byteArrayOutputStream.toByteArray();
            Log.i(TAG, "bytes length: " + bytes.length);

            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

            String newFileName = "newStaff" + index + ".jpg";
            File newFile = new File(getFilesDir(), newFileName);
            FileOutputStream fileOutputStream = new FileOutputStream(newFile);

            boolean compress = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
            Log.i(TAG, newFileName + " compress: " + compress);
        } catch (IOException e) {
            e.printStackTrace();
        }

        test_task();
    });
}
# 输出结果

bytes length: 31530
newStaff1.jpg compress: true
bytes length: 31530
newStaff2.jpg compress: true
bytes length: 31530
newStaff3.jpg compress: true
bytes length: 31530
newStaff4.jpg compress: true
bytes length: 31530
newStaff5.jpg compress: true
bytes length: 31530
newStaff6.jpg compress: true
bytes length: 31530
newStaff7.jpg compress: true
bytes length: 31530
newStaff8.jpg compress: true
bytes length: 31530
newStaff9.jpg compress: true
bytes length: 31530
newStaff10.jpg compress: true

你可能感兴趣的:(Android 图像处理 - Bitmap 图像处理观察记录(基本图像复制、带目录创建的图像复制、字节流处理的图像复制、并发图像复制、单线程池顺序图像复制))