java - 将图像转换为二维数组仅获取左上象限

标签 java android android-canvas android-bitmap

我正在 Android 中制作一个程序动画,它应该只拍摄一张 720x720 的图像,将其转换为二维颜色数组,然后单独绘制每个像素。我的代码的问题是它只需要左上象限并将其显示为整个图像,我无法弄清楚如何修复它。
代码、源图像和结果在这里:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new DrawPixels(this, 720, 720));

    }
} 


public class DrawPixels extends View {
    public int[][] image_array;
    Bitmap frame;
    Bitmap photo;
    Canvas frameDrawer;
    Rect bounds;
    Paint lp;
    int width , height;
    int x = 0;
    int y = 0;
    int radius = 3;

    public DrawPixels(Context context , int width, int height) {
        super(context);
        photo = BitmapFactory.decodeResource(context.getResources(), R.drawable.marilyn2);
        image_array = imageTo2dArray(photo);

        this.width = width;
        this.height = height;

        frame = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
        frameDrawer = new Canvas(frame);
        bounds = new Rect(0 , 0, width,height);

        lp = new Paint();
        lp.setStyle(Paint.Style.FILL);

    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);

        if (x < 720 && y < 720) {
            lp.setColor(image_array[x][y]);
            frameDrawer.drawCircle(x, y, radius, lp);
        }

        canvas.drawBitmap(frame, null, bounds , null);

        //sampling every 5th pixel
        if (x < 720){
            if(y < 720){
                y += 5;
            }else{
                y = 0;
                x += 5;
            }
            invalidate();
        }
    }


    public int[][] imageTo2dArray(Bitmap bm){
        int[][] arr = new int[720][720];
        int p, a, r, g, b, avg;
        for(int i = 0; i < 720; i++) {
            for (int j = 0; j < 720; j++) {
                arr[i][j] = bm.getPixel(i, j);
            }
        }
        return  arr;
    }
}


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="d.androidnewcomer.drawing.MainActivity">

</android.support.constraint.ConstraintLayout>

玛丽莲2.png: enter image description here

结果: enter image description here

最佳答案

最可能的原因是 BitmapFactory.decodeResource 缩放位图,使其不再是 720x720 而是 1440x1440。

参见:BitmapFactory.decodeResource Bitmap not original size in pixels

关于java - 将图像转换为二维数组仅获取左上象限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50085728/

相关文章:

java - 我的应用程序一直在运行 Full GC!

java - java中的BufferedWriter和socket,write无效

android - 如何用 RecyclerView 替换 Fragment

android - 什么在编程上等同于 android :layout_centerInParent ="true" of RelativeLayout?

Android:如何只为图像的特定部分填充颜色?

java - Hibernate 使用普通 POJO

java - 如何创建一个包含原始主键和复合主键作为表复合键的 JoinTable(Spring JPA)?

android - Gson 忽略 json 字段并反序列化

java - 将 Int 值传递给另一个类

android - 我的位图没有显示所有像素