ITeye博客

文字图片转成点阵的小工具

Function · 2011-12-26 10:40 · javase · 文字转成图片 · Markdown

无聊,写了一个小工具。 将汉字转成点阵,图片转成点阵,看下效果图:

左边的是在记事本里显示的效果,再来一张:

功能还很单一,只能一种颜色,便宜量就是你每次跳几个像素取点。 适用地点:做一些有效果的字,可以用txt的替换工具将点变成其他的。 比如:字为“某某我爱你”,转成点阵后,将字的部分替换为“O”(字母),空格的部分替换为“0”数字,然后发给心爱的人,教她做一个替换,再把字显示出来,嘿嘿。 其实,图片也可以处理,但是效果没有汉字好,色差大! 大家用的时候可以多尝试几个颜色,要是文字图片的话,取灰色就出效果了!

运行环境:JDK6

package com.liu;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Main extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2764274223326342315L;
	private JTextArea showArea;
	private JButton btnOk;

	private JFileChooser jfc;
	private JLabel jLabel3;
	private JTextField point;
	private JLabel jLabel2;
	private JLabel jLabel1;
	private JComboBox offsetbox;
	private JScrollPane jScrollPane1;
	private JButton btnColor;
	private JPanel jpcolor;
	private JButton choosefile;
	private JTextField filepathArea;

	private String YES = "*";

	private String NO = " ";

	private Integer[] cell = new Integer[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

	private JLabel jlImage = new JLabel();

	private File image;

	private Color color = new Color(0, 0, 0);

	public Main() {
		getContentPane().setLayout(null);
		jfc = new JFileChooser("wenjian");

		btnOk = new JButton();
		getContentPane().add(btnOk);
		btnOk.setText("ok");
		btnOk.setBounds(869, 12, 59, 27);

		jScrollPane1 = new JScrollPane();
		getContentPane().add(jScrollPane1);
		jScrollPane1.setBounds(458, 56, 470, 334);

		showArea = new JTextArea();

		jScrollPane1.setViewportView(showArea);
		showArea.setBounds(458, 56, 470, 334);

		filepathArea = new JTextField();
		getContentPane().add(filepathArea);
		filepathArea.setBounds(12, 12, 150, 27);

		choosefile = new JButton();
		getContentPane().add(choosefile);
		choosefile.setText("\u56fe\u7247 ");
		choosefile.setBounds(174, 12, 89, 27);

		jpcolor = new JPanel();
		getContentPane().add(jpcolor);
		jpcolor.setBounds(294, 17, 25, 22);
		jpcolor.setBackground(Color.BLACK);

		btnColor = new JButton();
		getContentPane().add(btnColor);
		btnColor.setText("\u53d6\u8272");
		btnColor.setBounds(331, 12, 82, 27);

		ComboBoxModel offsetboxModel = new DefaultComboBoxModel(cell);
		offsetbox = new JComboBox();
		getContentPane().add(offsetbox);
		offsetbox.setModel(offsetboxModel);
		offsetbox.setBounds(470, 12, 62, 27);

		jLabel1 = new JLabel();
		getContentPane().add(jLabel1);
		jLabel1.setText("px");
		jLabel1.setBounds(537, 15, 18, 20);

		jLabel2 = new JLabel();
		getContentPane().add(jLabel2);
		jLabel2.setText("\u504f\u79fb\u91cf");
		jLabel2.setBounds(418, 15, 45, 20);

		point = new JTextField();
		getContentPane().add(point);
		point.setBounds(675, 12, 45, 27);
		point.setText(YES);

		jLabel3 = new JLabel();
		getContentPane().add(jLabel3);
		jLabel3.setText("\u586b\u5145\u5b57\u7b26");
		jLabel3.setBounds(592, 15, 80, 20);

		btnColor.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				color = JColorChooser.showDialog(Main.this, "Color", color);
				if (color == null) {
					color = Color.BLACK;
				}
				jpcolor.setBackground(color);
				System.out.println(color.getRGB());
			}
		});

		choosefile.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				getContentPane().remove(jlImage);
				jfc.setDialogTitle("Open JPEG file");
				jfc.showOpenDialog(jfc);
				image = jfc.getSelectedFile();
				if (image == null)
					return;
				String path = image.getAbsolutePath();
				filepathArea.setText(path);
				jlImage = new JLabel(new ImageIcon(image.getAbsolutePath()));
				getContentPane().add(jlImage);
				jlImage.setBounds(0, 56, 470, 334);
			}
		});

		btnOk.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int offset = cell[offsetbox.getselectedindex()];
				showArea.setText("");
				String str = "";

				BufferedImage bi = null;
				try {
					bi = ImageIO.read(image);
				} catch (IOException e1) {
					e1.printStackTrace();
				}

				for (int i = offset / 2; i < bi.getHeight(); i += offset) {
					str = "";
					for (int j = offset / 2; j < bi.getWidth(); j += offset) {
						int pixel = bi.getRGB(j, i);
						if (pixel > color.getRGB()) {
							str += NO;
						} else {
							str += YES;
						}
					}
					showArea.append(str + "\n");
					System.out.println(str);
				}

			}
		});

		this.setSize(969, 453);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		showArea.setFont(new Font("黑体", Font.BOLD, 13));
	}

	public static void main(String[] args) {
		new Main();
	}

}

后面可以加上复合取色,这样图片的效果会好一点,小工具,欢迎交流

评论(5)

  1. #5 楼 · seasoon · 2011-12-26 18:03

    用来做十字绣很不错啊,哈哈

  2. #4 楼 · Function · 2011-12-26 16:10

    可以用PS设计自己的艺术字,最好粗一点,然后用这个工具进行转换,说用处到是不多,挺好玩的。

  3. #3 楼 · Function · 2011-12-26 16:05

    tron.lu writes:

    楼主不简单描述一下思路 流程~?

    这个就简单说一下,其实也不复杂,获取图片,创建BufferedImage对象,获取图片大小,然后按照偏移量逐行扫描,获取像素上的RGB值,和用户设置的RGB值比较,判断是否输出“*”。可以加上复合色的选取,用户设置多个颜色,然后取色时就可以把图片描述的更形象了。

  4. #2 楼 · tron.lu · 2011-12-26 14:10

    楼主不简单描述一下思路 流程~?

  5. #1 楼 · shidan66 · 2011-12-26 11:50

    不错啊,顶一个~

评论功能维护中,当前仅展示历史评论。