如何用设定的单词数使原始图像的单词云?

时间:2019-02-27 09:51:54

标签: java android typography word-cloud image-effects

如何制作图像的词云图像。

像上面的原始图片一样输出

enter image description here

我正在尝试使用https://github.com/bachors/Android-Img2Ascii

将图像粘贴到Ascii
 TextView textAsc=(TextView)findViewById(R.id.textAsc);
    Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.step0001);
// Bitmap image = BitmapFactory.decodeFile(filename);

    new Img2Ascii()
            .bitmap(image)
            .quality(4) // 1 - 5
            .color(true)

            .convert(new Img2Ascii.Listener() {
                @Override
                public void onProgress(int percentage) {
                    textAsc.setText(String.valueOf(percentage) + " %");
                }
                @Override
                public void onResponse(Spannable text) {
                    textAsc.setText(text);
                }
            });

img2ascii.java

package com.bachors.img2ascii;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static java.lang.Math.round;

/**
 * Created by Bachors on 10/31/2017.
 * https://github.com/bachors/Android-Img2Ascii
 */



public class Img2Ascii {

    private String[] chars = {"@", "#", "+", "\\", ";", ":", ",", ".", "`", " "};
    private Bitmap rgbImage;
    private Boolean color = false;
    private int quality = 3;
    private int qualityColor = 6;
    private Spannable response;
    private Listener listener;

    List<String> list = new ArrayList<String>();




    public Img2Ascii(){

    }

    public Img2Ascii bitmap(Bitmap rgbImage){
        this.rgbImage = rgbImage;
        return this;
    }

    public Img2Ascii quality(int quality){
        this.quality = quality;
        return this;
    }

    public Img2Ascii color(Boolean color){
        this.color = color;
        return this;
    }

    public void convert(Listener listener) {
        this.listener = listener;
        new InstaApi().execute();
    }

    @SuppressLint("StaticFieldLeak")
    private class InstaApi extends AsyncTask<String, Integer, Void> {

        private InstaApi(){

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            list.add("Cool");
            list.add("G");
            list.add("S");
            list.add("L");
        }

        @Override
        protected Void doInBackground(String... arg0) {
            if(color) {
                quality = quality + qualityColor;
                if (quality > 5 + qualityColor || quality < 1 + qualityColor)
                    quality = 3 + qualityColor;
            }else{
                if (quality > 5 || quality < 1)
                    quality = 3;
            }
            String tx;
            SpannableStringBuilder span = new SpannableStringBuilder();
            int width = rgbImage.getWidth();
            int height = rgbImage.getHeight();
            int i = 0;
            for (int y = 0; y < height; y = y + quality) {
                for (int x = 0; x < width; x = x + quality) {
                    int pixel = rgbImage.getPixel(x, y);
                    int red = Color.red(pixel);
                    int green = Color.green(pixel);
                    int blue = Color.blue(pixel);
                    if(color) {
                        Random randomizer = new Random();
                        String random =list.get(randomizer.nextInt(list.size()));
                        tx = random;
                        span.append(tx);
                        span.setSpan(new ForegroundColorSpan(Color.rgb(red, green, blue)), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }else {
                        int brightness = red + green + blue;
                        brightness = round(brightness / (765 / (chars.length - 1)));
                        tx = chars[brightness];
                        span.append(tx);
                    }
                    i++;
                }
                tx = "\n";
                span.append(tx);
                publishProgress(y, height);
                i++;
                if(isCancelled()) break;
            }
            response = span;

            return null;
        }

        protected void onProgressUpdate(Integer... progress) {
            int current = progress[0];
            int total = progress[1];
            int percentage = 100 * current / total;
            listener.onProgress(percentage);
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            listener.onResponse(response);
        }

    }

    public interface Listener {
        void onProgress(int percentage);
        void onResponse(Spannable response);
    }

}

0 个答案:

没有答案