【问题标题】:Flip character horizontally and print it with java水平翻转字符并用java打印
【发布时间】:2012-02-22 13:36:01
【问题描述】:

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

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

【问题讨论】:

  • 我不认为有任何方法可以做这样的事情,除非你得到键盘上每个符号的图片,手动进行图像旋转,然后将这些图像映射到与正确键对应的键盘事件按。还有人有更好的建议吗?

标签: java character flip mirror


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    使用图像实现;不知道有没有办法让角色自己翻转。

    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

    【讨论】:

      【解决方案3】:

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

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

      1. 创建一个 BufferedImage 对象

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

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

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

      我想如果你想让它与任何字体、多种尺寸等一起工作,你必须在一个程序中完成。

      更新

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

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

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

      【讨论】:

      • 我显然知道该怎么做。反正这个任务有很多教程和论坛……我没那么傻。我正在尝试的是镜像 CHARACTER,而不是它的 IMAGE。也就是说,找到每个字母的 unicode,如 \u0258 的 ɘ(这是 e 的镜像),这就是我想要完成的。不过对于整个字母表。
      • 我们无法衡量“你有多愚蠢”,因为你没有充实这个问题来表明你知道什么。 知道我们有多通灵吗? -1
      • 我所知道的是你“我真的不知道你在这里想要达到什么目标”的自大态度。如果这对回答我的问题有任何帮助,我会将其包含在我的帖子中。 :)
      • 嗯,这并不是自大,而是简单的事实陈述。我不清楚你的目标是什么,所以很难说任何给定的建议是否有帮助。无论是你的错还是我的错,我相信我们都没有充分沟通。正如我和我认为本论坛的大多数其他参与者一样,不认识您本人,我们无法知道您是完全的新手还是世界领先的 Java 专家。如果每次有人说,“对不起,我不明白你,你能不能再解释一下”你坚持认为这是一种侮辱,你会花很多时间不必要地生气。 :-)
      猜你喜欢
      • 2018-02-01
      • 2014-12-13
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多