【HDR学习】利用TextureView显示图片

demo场景:利用textureView实现图片显示,并更改其色彩空间(sRGB和DCI_P3)拼接显示。

package com.huawei.presearch.views;

import android.content.Context;
import android.graphics.Color;
import android.graphics.ColorSpace;
import android.graphics.SurfaceTexture;
import android.util.AttributeSet;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.TextureView;

import java.io.IOException;

public class PictureTextureView extends TextureView implements android.view.TextureView.SurfaceTextureListener {

    private static final String TAG = "PictureTextureView";
    private Paint mPaint;//画笔
    private Rect mSrcRect;
    private Rect mDstRect;
    private Thread thread;//用于绘制的线程
    private boolean isRunning;//线程的控制开关

    private Bitmap mBitmap;
    int mWidth;
    int mHeight;

    public PictureTextureView(Context context) {
        this(context, null);
    }

    public PictureTextureView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public PictureTextureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        Log.i(TAG, ": init() ");

        setSurfaceTextureListener(this);//设置监听
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//创建画笔

        mSrcRect = new Rect();
        mDstRect = new Rect();
    }
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        Log.i(TAG,": onSurfaceTextureAvailable()");
        Log.i(TAG,": width = " + width);
        Log.i(TAG,": height = " + height);
        mWidth = width;
        mHeight = height;
        isRunning = true;

        //开启线程
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                if (isRunning) {
                    String path = "wide-color-img/photo04.jpg";
                    try {
                        mBitmap = readBitmap(path);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    ColorSpace colorSpace = mBitmap.getColorSpace();
                    Bitmap.Config config = mBitmap.getConfig();

                    mergeBitmap_TB(mBitmap,mBitmap);
                }
            }
        });
        thread.start();
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        Log.i(TAG,": onSurfaceTextureSizeChanged()");
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        Log.i(TAG,": onSurfaceTextureDestroyed()");
        isRunning = false;
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        Log.i(TAG,": onSurfaceTextureUpdated()");
    }

    private Bitmap readBitmap(String path) throws IOException {
        Log.i(TAG, ": readBitmap() ");
        return BitmapFactory.decodeStream(getResources().getAssets().open(path));
    }

    private void drawBitmap_One(Bitmap bitmap) {
        Log.i(TAG, ": drawBitmap() ");
        Canvas canvas = lockCanvas();//锁定画布

        if (canvas != null) {
            canvas.drawColor(Color.BLACK);
            mSrcRect.set(0, 100, bitmap.getWidth(), bitmap.getHeight());//这里我将2个rect抽离出去,防止重复创建
            mDstRect.set(0, 100, mWidth, bitmap.getHeight() * mWidth / bitmap.getWidth());
            canvas.drawBitmap(bitmap, mSrcRect, mDstRect, mPaint);//将bitmap画到画布上
            unlockCanvasAndPost(canvas);//解锁画布同时提交
        }

    }

    /**
     * 把两个位图覆盖合成为一个位图,上下拼接
     * @param leftBitmap
     * @param rightBitmap
     * @param isBaseMax 是否以高度大的位图为准,true则小图等比拉伸,false则大图等比压缩
     * @return
     */
    private Bitmap mergeBitmap_TB(Bitmap topBitmap, Bitmap bottomBitmap) {

        if (topBitmap == null || topBitmap.isRecycled()
                || bottomBitmap == null || bottomBitmap.isRecycled()) {
            Log.e(TAG, "topBitmap=" + topBitmap + ";bottomBitmap=" + bottomBitmap);
            return null;
        }

        Rect topRect = new Rect(0, 0, topBitmap.getWidth(), topBitmap.getHeight()); //(0,0)->(x,y)矩阵坐标
        Rect topRectT = new Rect(0, 0, mWidth, topBitmap.getHeight() * mWidth / topBitmap.getWidth());
        Rect bottomRect  = new Rect(0, 0, bottomBitmap.getWidth(), bottomBitmap.getHeight());
        Rect bottomRectT  = new Rect(0, topBitmap.getHeight(), mWidth, bottomBitmap.getHeight() * mWidth / bottomBitmap.getWidth() + topBitmap.getHeight());

        Canvas canvas = lockCanvas();//锁定画布
        if (canvas != null) {
            canvas.drawColor(Color.BLACK);
            canvas.drawBitmap(topBitmap, topRect, topRectT, mPaint); //topRect表示图片子集;topRectT表示位图将缩放/转换以适应的矩形

            bottomBitmap.setColorSpace(ColorSpace.get(ColorSpace.Named.DCI_P3));
            canvas.drawBitmap(bottomBitmap, bottomRect, bottomRectT, mPaint);
            unlockCanvasAndPost(canvas);//解锁画布同时提交
        }
        return mBitmap;
    }
}

图片拼接参考:Android将两个bitmap合并_android bitmap合并_年少的风的博客-CSDN博客

图片来源下载:Wide Gamut - Images Page

猜你喜欢

转载自blog.csdn.net/cocoduco/article/details/129875883
HDR