【JAVA】OPENGL+TIFF格式图片,不同阈值旋转效果

2024-01-07 00:52

本文主要是介绍【JAVA】OPENGL+TIFF格式图片,不同阈值旋转效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

有些科学研究领域会用到一些TIFF格式图片,由于是多张图片相互渐变,看起来比较有意思:

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.*;/*** 可以自已定义日志打印格式,这样看起来比较方便些**/
class MyFormatter extends Formatter
{@Overridepublic String format(LogRecord arg0){//创建StringBuilder对象来存放后续需要打印的日志内容StringBuilder builder = new StringBuilder();//获取时间SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");Date now = new Date();String dateStr = simpleDateFormat.format(now);builder.append("[");builder.append(dateStr);builder.append(" ");//拼接日志级别builder.append(arg0.getLevel()).append(" ");builder.append(arg0.getSourceClassName()).append(" ");//拼接方法名builder.append(arg0.getSourceMethodName()).append(" ");StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();String line = stackTrace[8].toString();String lineNumber = line.substring(line.indexOf(":") + 1, line.length() - 1);//拼接方法名builder.append(lineNumber).append("] ");//拼接日志内容builder.append(arg0.getMessage());//日志换行builder.append("\r\n");return builder.toString();}
}public class MyLogger {static Logger logger;static  {logger = Logger.getLogger(MyLogger.class.getName());logger.setUseParentHandlers(false);//如果需要将日志文件写到文件系统中,需要创建一个FileHandler对象Handler consoleHandler = new ConsoleHandler();//创建日志格式文件:本次采用自定义的FormatterconsoleHandler.setFormatter(new MyFormatter());//将FileHandler对象添加到Logger对象中logger.addHandler(consoleHandler);}public static Logger getLogger() {return logger;}public static void main(String[] args) {MyLogger.logger.info("1");Logger logger = MyLogger.logger;logger.info("2");}
}

import com.sun.media.jai.codec.*;import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.TIFFEncodeParam;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.JPEGEncodeParam;
import java.awt.image.RenderedImage;
import javax.media.jai.RenderedOp;
import javax.media.jai.JAI;import java.awt.*;
import java.awt.image.DataBuffer;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.*;//本类继承自画布类,用作绘图的面板,因为Java不允许多继承,所以要用此类
class TIFBase extends Canvas
{ImageDecoder dec;TIFBase() throws IOException {this.TifRead();}public void TifRead() throws IOException{String currentDir = System.getProperty("user.dir");System.out.println("当前目录:" + currentDir);//FileSeekableStream fileSeekableStream = new FileSeekableStream("human_brain_from_itk_example.tif");FileSeekableStream fileSeekableStream = new FileSeekableStream("ex_Repo_hb9_eve1.tif");TIFFDecodeParam param0 = null;TIFFEncodeParam param = new TIFFEncodeParam();JPEGEncodeParam param1 = new JPEGEncodeParam();dec = ImageCodec.createImageDecoder("tiff", fileSeekableStream, param0);param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);param.setLittleEndian(false); // Intel}public ImageDecoder getDec() {return dec;}public void setDec(ImageDecoder dec) {this.dec = dec;}public void TifDisplay(Graphics g) throws IOException, InterruptedException{int pagesCount = dec.getNumPages();System.out.println("This TIF has " + pagesCount + " image(s)");System.out.println();for (int i = 0; i < pagesCount; i++){System.out.println("image: " + i);RenderedImage page = dec.decodeAsRenderedImage(i);DataBuffer dataBuffer = page.getData().getDataBuffer();//System.out.println("size: " + dataBuffer.getSize());int height = page.getHeight();int width = page.getWidth();//g.drawString(page.getData().toString(), 0, 0);for (int j = 0; j < height; j++){for (int k = 0; k < width; k++){int red = dataBuffer.getElem((j * width + k) * 3);int green = dataBuffer.getElem((j * width + k) * 3 + 1);int blue = dataBuffer.getElem((j * width + k) * 3 + 2);g.setColor(new Color(red, green, blue));g.drawOval(j, k, 1, 1);}}}//Thread.sleep(10);}public void TifDisplay2(Graphics g) throws IOException, InterruptedException {int pagesCount = dec.getNumPages();System.out.println("This TIF has " + pagesCount + " image(s)");System.out.println();DataBuffer[] dataBuffers = new DataBuffer[pagesCount];int height = 0;int width = 0;for (int i = 0; i < pagesCount; i++){//System.out.println("image: " + i);RenderedImage page = dec.decodeAsRenderedImage(i);//System.out.println("height: " + page.getHeight() + ", width: " + page.getWidth());height = page.getHeight();width = page.getWidth();dataBuffers[i] = page.getData().getDataBuffer();}int statPage = 0;int endPage = pagesCount;int gap = endPage - statPage;int miniColor = 3;for (int i = 0; i < height; i++){for (int j = 0; j < width; j++){int redSumary = 0;int greenSumary = 0;int blueSumary = 0;int validRedColorFlag = 0;int validGreenColorFlag = 0;int validBlueColorFlag = 0;for (int k = statPage; k < endPage; k++){int red = dataBuffers[k].getElem((i * width + j) * 3);int green = dataBuffers[k].getElem((i * width + j) * 3 + 1);int blue = dataBuffers[k].getElem((i * width + j) * 3 + 2);if (red > miniColor){redSumary += red;validRedColorFlag++;}if (green > miniColor){greenSumary += green;validGreenColorFlag++;}if (blue > miniColor){blueSumary += blue;validBlueColorFlag++;}}redSumary = (validRedColorFlag == 0) ? 0 : (redSumary / validRedColorFlag);greenSumary = (validGreenColorFlag == 0) ? 0 : (greenSumary / validGreenColorFlag);blueSumary = (validBlueColorFlag == 0) ? 0 : (blueSumary / validBlueColorFlag);g.setColor(new Color(redSumary, greenSumary, blueSumary));g.drawOval(i, j, 1, 1);}}}// 把窗口拉宽些就可。public void paint(Graphics g){System.out.println("1");try {this.TifDisplay(g);//this.TifDisplay2(g);}catch (IOException | InterruptedException e){throw new RuntimeException(e);}}
}public class TIF extends JFrame
{public TIF(){super("画直线");this.setVisible(true);this.setBounds(200, 200, 600, 600);}public void Display() throws IOException{//创建对象TIFBase tifBase = new TIFBase();//创建实例this.getContentPane().add(tifBase);}public static void main(String[] args) throws IOException{TIF tif = new TIF();tif.Display();}
}
import com.sun.media.jai.codec.ImageDecoder;
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;import java.awt.*;
import java.awt.image.DataBuffer;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.nio.*;
import java.util.Objects;
import java.util.logging.Logger;import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL32.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;public class HelloWorld {// The window handleprivate long window;public static Logger logger = MyLogger.logger;private float angle;private ImageDecoder dec;private int threshold;HelloWorld()  {//创建对象try {TIFBase tifBase = new TIFBase();//创建实例dec = tifBase.getDec();} catch (IOException e) {throw new RuntimeException(e);}}public void run(){logger.info("Hello LWJGL " + Version.getVersion() + "!");init();loop();// Free the window callbacks and destroy the windowglfwFreeCallbacks(window);glfwDestroyWindow(window);// Terminate GLFW and free the error callbackglfwTerminate();Objects.requireNonNull(glfwSetErrorCallback(null)).free();}private void init() {logger.info("init");// Setup an error callback. The default implementation// will print the error message in System.err.GLFWErrorCallback.createPrint(System.err).set();angle = 5;threshold = 20;// Initialize GLFW. Most GLFW functions will not work before doing this.if ( !glfwInit() )throw new IllegalStateException("Unable to initialize GLFW");// Configure GLFWglfwDefaultWindowHints(); // optional, the current window hints are already the defaultglfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creationglfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable// Create the windowwindow = glfwCreateWindow(600, 600, "Hello World!", NULL, NULL);if ( window == NULL )throw new RuntimeException("Failed to create the GLFW window");// Setup a key callback. It will be called every time a key is pressed, repeated or released.glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop});// Get the thread stack and push a new frametry ( MemoryStack stack = stackPush() ) {IntBuffer pWidth = stack.mallocInt(1); // int*IntBuffer pHeight = stack.mallocInt(1); // int*// Get the window size passed to glfwCreateWindowglfwGetWindowSize(window, pWidth, pHeight);// Get the resolution of the primary monitorGLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());// Center the windowglfwSetWindowPos(window,(vidmode.width() - pWidth.get(0)) / 2,(vidmode.height() - pHeight.get(0)) / 2);} // the stack frame is popped automatically// Make the OpenGL context currentglfwMakeContextCurrent(window);// Enable v-syncglfwSwapInterval(1);// Make the window visibleglfwShowWindow(window);}/*** 绘制三角形*/public void DrawTriangles(){glTranslatef(-0.5f, -0.5f, -0.00f); //平移矩阵glRotatef(angle, 0.5f, 0.5f, 0.0f); //绕X,Y, Z轴直线旋转XX度glPointSize(1.0f);glBegin(GL_POINTS);try {int pagesCount = dec.getNumPages();//System.out.println("This TIF has " + pagesCount + " image(s)");//System.out.println();for (int i = 0; i < pagesCount; i++){//System.out.println("image: " + i);RenderedImage page = dec.decodeAsRenderedImage(i);DataBuffer dataBuffer = page.getData().getDataBuffer();//System.out.println("size: " + dataBuffer.getSize());int height = page.getHeight();int width = page.getWidth();//g.drawString(page.getData().toString(), 0, 0);for (int j = 0; j < height; j++){for (int k = 0; k < width; k++){int red = dataBuffer.getElem((j * width + k) * 3);int green = dataBuffer.getElem((j * width + k) * 3 + 1);int blue = dataBuffer.getElem((j * width + k) * 3 + 2);//g.setColor(new Color(red, green, blue));//System.out.println("red: " + red + ", green: " + green + ", blue: " + blue);if (red > threshold ){glColor3b((byte) red, (byte) 0, (byte) 0);glVertex3f(k / 200.0f,j / 200.0f,i / 200.0f);}if (green > threshold ){glColor3b((byte) 0, (byte) green, (byte) 0);glVertex3f(k / 200.0f,j / 200.0f,i / 200.0f);}if (blue > threshold ){glColor3b((byte) 0, (byte) 0, (byte) blue);glVertex3f(k / 200.0f,j / 200.0f,i / 200.0f);}//g.drawOval(j, k, 1, 1);}}}} catch (IOException e) {throw new RuntimeException(e);}System.out.println("threshold: " + threshold  + ", angle: " + angle);if (angle >= 350){angle = 0;threshold += 10;}angle+=10;glEnd();}public void DrawLines(){glBegin(GL_LINES);glVertex2i(0, 0);glVertex2i(0, 1);glEnd();}public void DrawQuads(){glBegin(GL_QUADS);//顶面glColor3f(0.0f, 1.0f, 0.0f);glVertex3f(1.0f, 1.0f, -1.0f);     //右上顶点glVertex3f(-1.0f, 1.0f, -1.0f);    //左上顶点glVertex3f(-1.0f, 1.0f, 1.0f);     //左下顶点glVertex3f(1.0f, 1.0f, 1.0f);      //右下顶点//底面glColor3f(1.0f, 0.5f, 0.0f);glVertex3f(1.0f, -1.0f, 1.0f);     //右上顶点glVertex3f(-1.0f, -1.0f, 1.0f);    //左上顶点glVertex3f(-1.0f, -1.0f, -1.0f);   //左下顶点glVertex3f(1.0f, -1.0f, -1.0f);    //右下顶点//前面glColor3f(1.0f, 0.0f, 0.0f);glVertex3f(1.0f, 1.0f, 1.0f);      //右上顶点glVertex3f(-1.0f, 1.0f, 1.0f);     //左上顶点glVertex3f(-1.0f, -1.0f, 1.0f);    //左下顶点glVertex3f(1.0f, -1.0f, 1.0f);     //右下顶点//后面glColor3f(1.0f, 1.0f, 0.0f);glVertex3f(1.0f, -1.0f, -1.0f);    //右上顶点glVertex3f(-1.0f, -1.0f, -1.0f);   //左上顶点glVertex3f(-1.0f, 1.0f, -1.0f);    //左下顶点glVertex3f(1.0f, 1.0f, -1.0f);     //右下顶点//左侧面glColor3f(0.0f, 0.0f, 1.0f);glVertex3f(-1.0f, 1.0f, 1.0f);     //右上顶点glVertex3f(-1.0f, 1.0f, -1.0f);    //左上顶点glVertex3f(-1.0f, -1.0f, -1.0f);   //左下顶点glVertex3f(-1.0f, -1.0f, 1.0f);     //右下顶点//右侧面glColor3f(1.0f, 0.0f, 1.0f);glVertex3f(1.0f, 1.0f, -1.0f);     //右上顶点glVertex3f(1.0f, 1.0f, 1.0f);      //左上顶点glVertex3f(1.0f, -1.0f, 1.0f);     //左下顶点glVertex3f(1.0f, -1.0f, -1.0f);    //右下顶点glEnd();}private void loop() {logger.info("loop");// This line is critical for LWJGL's interoperation with GLFW's// OpenGL context, or any context that is managed externally.// LWJGL detects the context that is current in the current thread,// creates the GLCapabilities instance and makes the OpenGLGL.createCapabilities();//DrawQuads();//-----------------------------------------//glLoadIdentity();   //重置当前的模型观察矩阵//————————————————//版权声明:本文为CSDN博主「贝勒里恩」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。//原文链接:https://blog.csdn.net/Mr_robot_strange/article/details/123682686while (true){//glClearColor(1.0f, 1.0f, 0.0f, 0.0f);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除屏幕和深度缓存//-----------------------------------------glLoadIdentity();   //重置当前的模型观察矩阵//DrawLines();DrawTriangles();glfwSwapBuffers(window);glfwPollEvents();}}public static void main(String[] args) {logger.info("main");new HelloWorld().run();}}

这上边的代码如果需要运行,需要依赖这些JAR包: 

运行效果如下:

TIFF格式图片文件不同阈值旋转_哔哩哔哩_bilibili

这篇关于【JAVA】OPENGL+TIFF格式图片,不同阈值旋转效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/578214

相关文章

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

解决java.lang.NullPointerException问题(空指针异常)

《解决java.lang.NullPointerException问题(空指针异常)》本文详细介绍了Java中的NullPointerException异常及其常见原因,包括对象引用为null、数组元... 目录Java.lang.NullPointerException(空指针异常)NullPointer

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

Java实战之利用POI生成Excel图表

《Java实战之利用POI生成Excel图表》ApachePOI是Java生态中处理Office文档的核心工具,这篇文章主要为大家详细介绍了如何在Excel中创建折线图,柱状图,饼图等常见图表,需要的... 目录一、环境配置与依赖管理二、数据源准备与工作表构建三、图表生成核心步骤1. 折线图(Line Ch

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

Spring Boot 3 整合 Spring Cloud Gateway实践过程

《SpringBoot3整合SpringCloudGateway实践过程》本文介绍了如何使用SpringCloudAlibaba2023.0.0.0版本构建一个微服务网关,包括统一路由、限... 目录引子为什么需要微服务网关实践1.统一路由2.限流防刷3.登录鉴权小结引子当前微服务架构已成为中大型系统的标

Python脚本实现图片文件批量命名

《Python脚本实现图片文件批量命名》这篇文章主要为大家详细介绍了一个用python第三方库pillow写的批量处理图片命名的脚本,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言源码批量处理图片尺寸脚本源码GUI界面源码打包成.exe可执行文件前言本文介绍一个用python第三方库pi

Java集合中的List超详细讲解

《Java集合中的List超详细讲解》本文详细介绍了Java集合框架中的List接口,包括其在集合中的位置、继承体系、常用操作和代码示例,以及不同实现类(如ArrayList、LinkedList和V... 目录一,List的继承体系二,List的常用操作及代码示例1,创建List实例2,增加元素3,访问元