Use JavaScript to modify the resolution (DPI) of Canvas images
Application scenario:
The warehouse needs to print labels for each shipment. Canvas can generate label JPG based on the product information read from the database, but the default resolution (DPI) of this JPG image is 72
This DPI is too low, resulting in The printed image will be very blurry.
The normal way to modify the DPI is to upload the image to the server and use C# to modify the DPI and then download it to the server
But I feel aggrieved because the image has clearly been generated on the client, just In order to modify a small mark (DPI information is just a header META in JPEG format), I had to upload a large picture to the server.
Later I used the following method
Modify it directly with JavaScript DPI
The source code is not posted here, only solution ideas are provided. I hope friends who need it can try it themselves
Now that we know that DPI is only the header information of the JPEG format, and it is a very small part, and base64 is a stream encoding, Then in the base64 string after canvas.toDataURL(), the position of this DPI information should also be fixed
Use an image processing program (I use Fireworks) to generate two JPG images with the same content, only modify The DPI, the DPI of picture A is 72, and the DPI of picture B is 300
For confirmation, use UE to compare the two files. It is true that only the first few bytes are different
Use C# to read A/B Pictures are encoded into BASE64 respectively to generate A.txt and B.txt
Use UE or other text editors to compare the two text files and find the different parts. You will find that there are less than 10 characters near the head. It’s different
Repeat the above experiment and use different pictures, you will find that the fixed characters are always different in the end, and as long as the DPI is the same, the characters at the fixed position are also the same
Use canvas on the browser side .toDataURL generates the base64 characters of the jpg image, and then changes the character at the fixed position to the characteristic character with a DPI of 300 during the test
OK, now you have obtained a JPG image with a DPI of 300.
Friends who are interested can study BASE64 encoding, and then make a function to calculate the characteristic characters corresponding to different DPIs
As for the binary states, click on it with a calculator and you will find that it is direct storage DPI value
MORE:
In the company's projects, operators are used to saving different batches of images in a fixed folder. When the images generated by canvas are saved as, the default file name is It’s always canvas.jpg
The experience will be much better if they use the batch number.jpg they are used to as the default file name for saving
There are two solutions here,
Go to Google to find one called Downloadify's Flash component can ensure that when you click the "Download" button, a dialog box for selecting the save location will pop up, and the default file name can be specified with js
Use the HTML5 download attribute to add it to the A tag. This solution is simpler. However, Firefox/Chrome will save directly to the default download location without allowing the user to choose, and right-clicking Save As will lose the role of the default file name. Both options have their own pros and cons. It depends on the operator’s usage habits.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.
