水平翻转字符并用java打印

发布于 2024-12-29 14:25:31 字数 206 浏览 2 评论 0原文

我想做的是从键盘接收一个字符,例如“a”,然后水平翻转该字母并将其打印出来。我到处搜索翻转图像,但似乎没有任何方法可以做到这一点。有人可以帮助我吗?顺便说一句,我正在尝试用 Java 来做到这一点。 非常感谢,如果您需要更多详细信息,请在评论部分询问我。

ps 我无法向您显示所需的输出,因为我在网上找不到任何翻转的字符。 这个概念与图像水平翻转相同,但我想对字符执行此操作。

What I'm trying to do is receive a character from the keyboard, for example 'a' and flip that letter horizontally and print it out. I have searched everywhere for flipped images but there doesn't seem to exist any method to do that. Can anyone help me? I'm trying to do this in Java btw.
Thanks a lot in advance, if you need more details ask me in the comment section.

p.s. I can't show you the desired output because I couldn't find any flipped characters online.
The concept is the same as with an image horizontal flip, but I want to do this with characters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

说好的呢 2025-01-05 14:25:31

您可以尝试创建 BufferedImage、获取其 Graphics2D 并将其 AffineTransform 设置为 AffineTransform.scale(-1, 1),然后使用 Graphics2DdrawString 方法。你将无法将其打印到控制台 - 我认为你所建议的打印到控制台根本不可能 - 但该技术将为你生成图像。

You might try something like creating a BufferedImage, getting its Graphics2D and setting its AffineTransform to AffineTransform.scale(-1, 1), and then using the Graphics2D's drawString method. You're not going to be able to print that to the console -- I don't think what you're suggesting is possible at all, printing to the console -- but that technique would produce an image for you.

饮湿 2025-01-05 14:25:31

使用图像实现;不确定是否有办法翻转角色本身。

import java.io.*;

public class RotateAndFlip {


    static JPEGImage rotate90(JPEGImage inputImage) {
    JPEGImage outputImage = new JPEGImage(inputImage.getHeight(),
                          inputImage.getWidth());

    // Code to make outputImage into an image that is a 
    // 90 degree rotation of inputImage

    return outputImage;
    }
    static JPEGImage rotate180(JPEGImage inputImage) {
    return rotate90(rotate90(inputImage));
    }

    static JPEGImage rotate270(JPEGImage inputImage) {
    return rotate90(rotate90(rotate90(inputImage)));
    }

    static JPEGImage flipHorizontal(JPEGImage inputImage) {
    JPEGImage outputImage = new JPEGImage(inputImage.getWidth(),
                          inputImage.getHeight());

    // Code to make outputImage into an image that is a 
    // horizontal flip of inputImage

    return outputImage;
    }

    static JPEGImage flipVertical(JPEGImage inputImage) {
    return rotate90(flipHorizontal(rotate270(inputImage)));
    }

    public static void main (String args[]) {
    /* Check that the user has provided the right number of arguments */
    if (args.length != 1) {
        System.out.println("Usage: java JPEGCopy <source JPEG file> " + 
                   "<target JPEG file>");
        System.exit(1);
    }

    /* Create an empty image. We will read an image from a file into
       this object */
    JPEGImage imageOne = new JPEGImage();

    /* Try to open the file. This may cause an exception if the name 
       given is not a valid JPEG file so we need to catch the exceptions */
    try {
        imageOne.read(args[0]);
    } catch (Exception e) {
        /* An exception has been thrown. This is usually because the file
           either does not exist, or is not a JPEG image */
        System.out.println("Error reading file " + args[0]);
        System.out.println(e.getMessage());
        System.exit(1);
    }

    /* Make a new image to store the results in */
    JPEGImage imageTwo = new JPEGImage();


    /* Rotate by 90 degrees and write to a file */
    imageTwo = rotate90(imageOne);
    try {
        imageTwo.write("rotate90.jpg");
    } catch (Exception e) {
        System.out.println("Error writing file rotate90.jpg");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    /* Rotate by 180 degrees and write to a file */
    imageTwo = rotate180(imageOne);
    try {
        imageTwo.write("rotate180.jpg");
    } catch (Exception e) {
        System.out.println("Error writing file rotate180.jpg");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    /* Rotate by -90 degrees and write to a file */
    imageTwo = rotate270(imageOne);
    try {
        imageTwo.write("rotate270.jpg");
    } catch (Exception e) {
        System.out.println("Error writing file rotate270.jpg");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    /* Flip horizontally and write to a file */
    imageTwo = flipHorizontal(imageOne);
    try {
        imageTwo.write("flipHorizontal.jpg");
    } catch (Exception e) {
        System.out.println("Error writing file flipHorizontal.jpg");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    /* Flip vertically and write to a file */
    imageTwo = flipVertical(imageOne);
    try {
        imageTwo.write("flipVertical.jpg");
    } catch (Exception e) {
        System.out.println("Error writing file flipVertical.jpg");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    }
}

这里:http://www.cs.nott.ac.uk/ ~smx/IVIPracticals/exercise1.html

Implementation using images; not sure if there's a way to flip the characters themselves.

import java.io.*;

public class RotateAndFlip {


    static JPEGImage rotate90(JPEGImage inputImage) {
    JPEGImage outputImage = new JPEGImage(inputImage.getHeight(),
                          inputImage.getWidth());

    // Code to make outputImage into an image that is a 
    // 90 degree rotation of inputImage

    return outputImage;
    }
    static JPEGImage rotate180(JPEGImage inputImage) {
    return rotate90(rotate90(inputImage));
    }

    static JPEGImage rotate270(JPEGImage inputImage) {
    return rotate90(rotate90(rotate90(inputImage)));
    }

    static JPEGImage flipHorizontal(JPEGImage inputImage) {
    JPEGImage outputImage = new JPEGImage(inputImage.getWidth(),
                          inputImage.getHeight());

    // Code to make outputImage into an image that is a 
    // horizontal flip of inputImage

    return outputImage;
    }

    static JPEGImage flipVertical(JPEGImage inputImage) {
    return rotate90(flipHorizontal(rotate270(inputImage)));
    }

    public static void main (String args[]) {
    /* Check that the user has provided the right number of arguments */
    if (args.length != 1) {
        System.out.println("Usage: java JPEGCopy <source JPEG file> " + 
                   "<target JPEG file>");
        System.exit(1);
    }

    /* Create an empty image. We will read an image from a file into
       this object */
    JPEGImage imageOne = new JPEGImage();

    /* Try to open the file. This may cause an exception if the name 
       given is not a valid JPEG file so we need to catch the exceptions */
    try {
        imageOne.read(args[0]);
    } catch (Exception e) {
        /* An exception has been thrown. This is usually because the file
           either does not exist, or is not a JPEG image */
        System.out.println("Error reading file " + args[0]);
        System.out.println(e.getMessage());
        System.exit(1);
    }

    /* Make a new image to store the results in */
    JPEGImage imageTwo = new JPEGImage();


    /* Rotate by 90 degrees and write to a file */
    imageTwo = rotate90(imageOne);
    try {
        imageTwo.write("rotate90.jpg");
    } catch (Exception e) {
        System.out.println("Error writing file rotate90.jpg");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    /* Rotate by 180 degrees and write to a file */
    imageTwo = rotate180(imageOne);
    try {
        imageTwo.write("rotate180.jpg");
    } catch (Exception e) {
        System.out.println("Error writing file rotate180.jpg");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    /* Rotate by -90 degrees and write to a file */
    imageTwo = rotate270(imageOne);
    try {
        imageTwo.write("rotate270.jpg");
    } catch (Exception e) {
        System.out.println("Error writing file rotate270.jpg");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    /* Flip horizontally and write to a file */
    imageTwo = flipHorizontal(imageOne);
    try {
        imageTwo.write("flipHorizontal.jpg");
    } catch (Exception e) {
        System.out.println("Error writing file flipHorizontal.jpg");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    /* Flip vertically and write to a file */
    imageTwo = flipVertical(imageOne);
    try {
        imageTwo.write("flipVertical.jpg");
    } catch (Exception e) {
        System.out.println("Error writing file flipVertical.jpg");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    }
}

Here: http://www.cs.nott.ac.uk/~smx/IVIPracticals/exercise1.html

迷鸟归林 2025-01-05 14:25:31

不确定你想要实现什么目标。我认为最简单的方法是获取一组所有字母和符号的图像,使用任何传统的图形软件(PaintShopPro、PhotoShop 等)翻转它们,将图像保存为一堆单独的 GIF 或PNG 文件,然后在需要时检索适当的文件。

如果你真的想在Java程序中进行翻转,我认为关键是:

  1. 创建一个BufferedImage对象

  2. 对其调用createGraphics以获取一个Graphics对象。

    p>

  3. 使用drawString 将一个或多个字符写入此Graphics 对象

  4. 然后您可以使用getRGB 和setRGB 来操纵图像。基本上,您希望从上到中心遍历图像,从上半部分到下半部分交换像素。

我想如果你想让它适用于任何字体、多种尺寸等,你就必须在程序中完成。

更新

根据您1月30日的评论,看来您的意思是您不想自己生成反转图像,而是想在Unicode字符集中找到反转图像。据我所知,恐怕 Unicode 并不包含每个字符的反转图像。它包括一些本身就是符号并且具有特定含义的符号。例如,Unicode 在 U+2203 处有一个向后大写的 E,但如果您查看具有附近 Unicode 代码点的其他字符,您会发现它们是各种数学符号。倒着的 E 表示“在那里”,因为它是一个数学符号,意思是“存在”,而不是因为 Unicode 的发明者给出了每个字符的镜像。

我当然没有记住完整的 Unicode 代码集,所以我不能发誓某处不存在这样的字符集,但我真的很怀疑。 Unicode 的目的应该是提供一种表示世界上所有字母的方法以及一组强大的常用符号。他们非常刻意地没有为斜体和粗体等内容或不同大小的内容包含单独的代码点,因为这些内容应该通过选择不同的字体来处理,而不是一组不同的 Unicode 代码点。因此,如果他们添加每个符号的镜像,那将是非常令人惊讶的。这将违背 Unicode 的理念。如果他们这样做了,那为什么不颠倒版本呢?为什么不旋转90度呢?等等,

所以我认为你的问题的简短答案是,这是不可能完成的。没有这样的 Unicode 字符。

Not sure what you're trying to accomplish. I think the simplest way to do this would be to get a set of images of all the letters and symbols, use flip them using any conventional graphics software -- PaintShopPro, PhotoShop, whatever -- save the images as a bunch of individual GIF or PNG files, and then retrieve the appropriate one when you need it.

If you really want to do the flipping within the Java program, I think the key is to:

  1. Create a BufferedImage object

  2. Call createGraphics on it to get a Graphics object.

  3. Write the character or characters to this Graphics object using drawString

  4. Then you can manipulate the image however you want using getRGB and setRGB. Basically you'd want to traverse the image from top to center swapping pixels from the top half to the bottom half.

I guess if you want it to work with any font, in many sizes, etc, you'd have to do it in a program.

Update

Based on your comment of Jan 30, it appears that you mean that you don't want to generate the reversed image yourself, but rather you want to find a reversed image among the Unicode character set. I'm afraid that, to the best of my knowledge, Unicode does not include a reverse image of every character. It includes a few that are symbols in their own right and that have specific meanings. For example, Unicode has a backwards capital E at U+2203, but if you look at other characters with nearby Unicode code points you'll see that they are various mathematical symbols. A backwards E is "in there" because it's a math symbol meaning "there exists", not because the inventors of Unicode are giving mirror images of every character.

I certainly don't have the complete Unicode code set memorized so I can't swear that there's no such set of characters in there somewhere, but I really doubt it. The point of Unicode was supposed to be to give a way to represent all the alphabets in the world and a robust set of commonly-used symbols. They very deliberately did NOT include separate code points for things like italic and bold, or for different sizes, because those things should be handled by selecting a different font, not a different set of Unicode code points. It would thus be very surprising if they added mirror images of every symbol. That would be contrary to the philosophy of Unicode. And if they did that, then why not upside down versions? Why not rotated 90 degrees? Etc etc.

So I think the short answer to your question is, It can't be done. There are no such Unicode characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文