【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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/327398.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python课程设计基于python的人脸识别佩戴口罩系统设计

wx供重浩&#xff1a;创享日记 对话框发送&#xff1a;python口罩 获取完整论文报告源码源文件 1 研究背景与意义 新型冠状病毒展现出全球化流行和蔓延的趋势&#xff0c;这提醒我们&#xff1a;传染病防治在今后相当长时间内仍是疾病预测控制工作的重点。戴口罩是预防呼吸道…

网络实训模拟考察题目和答案(华为eNSP综合实验考试)

拓扑中四个交换机五个路由器&#xff0c;共九个设备 答案是对应的九个脚本&#xff08;从设备命名到保存&#xff09; 全部复制粘贴后&#xff0c;从PC1、PC2都是能Ping通服务器的&#xff08;保及格&#xff09;&#xff0c;其他要求没检查 题目 VLAN信息 设备名称端口链路…

ssm基于JSP的明水县苹果网吧计费管理系统的设计与实现论文

摘 要 现代经济快节奏发展以及不断完善升级的信息化技术&#xff0c;让传统数据信息的管理升级为软件存储&#xff0c;归纳&#xff0c;集中处理数据信息的管理方式。本明水县苹果网吧计费管理系统就是在这样的大环境下诞生&#xff0c;其可以帮助管理者在短时间内处理完毕庞大…

回顾2023编程之旅

一、前言 看在给了我一个博客专家的份上就继续写写博客&#xff0c;实事求是的讲如果是工作之余去总结csdn写写技术博客&#xff0c;还想混个专家什么的&#xff0c;真的是精力不够。因为里面的灌水的实在太多&#xff0c;比不过的&#xff0c;写这个玩意必须得淡泊名利才能悠然…

ROS-[ERROR] [1704543158.960463911]: length [stent_height] is not a valid float

出现如图所示报错 原因&#xff1a;未能正确调用&#xff0c;忘记加${}了

我建立了一个资源分享群

我建立了一个资源分享群 在为寻找资源犯愁&#xff1f; 在为分享资源犯愁&#xff1f; 一起加入分享资源群&#xff08;是wx群哦&#xff09;吧&#xff01;你可以分享自己的资源帮助他人。你可以在群组里需求资源获取别人的帮助。发广告请绕行&#xff0c;会被拉黑哦 微信…

<HarmonyOS主题课>1~3课后习题汇总

&#xff1c;HarmonyOS第一课&#xff1e;1~10课后习题汇总 1使用DevEco Studio高效开发 单选题 用哪一种装饰器修饰的组件可作为页面入口组件&#xff1f;&#xff08;B&#xff09; A. ComponentB. EntryC. PreviewD. Builder ArkTS Stage模型支持API Version 9&#xf…

网络请求 - 异步编程详解

一、概述 网络管理模块主要提供以下功能&#xff1a; HTTP数据请求&#xff1a;通过HTTP发起一个数据请求。WebSocket连接&#xff1a;使用WebSocket建立服务器与客户端的双向连接。Socket连接&#xff1a;通过Socket进行数据传输。 HTTP和WebSocket都是啥&#xff1f; 比如我…

MongoDB索引详解

概述 索引是一种用来快速查询数据的数据结构。BTree 就是一种常用的数据库索引数据结构&#xff0c;MongoDB 采用 BTree 做索引&#xff0c;索引创建 colletions 上。MongoDB 不使用索引的查询&#xff0c;先扫描所有的文档&#xff0c;再匹配符合条件的文档。使用索引的查询&…

web期末作业数字时钟,实时更新,音乐播放

文章目录 月球动态引导页加载引导页主页面主页面html需要完整代码私信我 月球动态引导页 加载引导页 主页面 主页面html <!DOCTYPE html> <html lang"zh-CN"><head><meta http-equiv"X-UA-Compatible" content"IEedge,chrome1&…

Socket closed 异常解决方案:如何解决 JMeter 压测中的问题

问题描述 JMeter 压测时会报 java.net.SocketException: Socket closed java.net.SocketException: Socket closed at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.ne…

关于vite的glob坑

我先展示一段代码&#xff1a; /*** function 根据pages路径动态生成路由* param {Array} 基础路由*/ export default function (routes) {const modules import.meta.glob("../pages/**/page.js", { eager: true, import: "default" });const newRoutes…