The barrier coverage problem is one of the most important issues in wireless sensor networks (WSNs). In previous research, scholars did not consider the actual density of sensors in the area. As a result, their algorithms might require sensors to move a longer distance to form a barrier. A longer distance of movement means more energy consumption. This requirement is disadvantageous to sensors with limited energy. In this paper, we consider the actual density of sensors in solving the barrier coverage problem in an attempt to minimize the moving distances of mobile sensors and further enhance the network lifetime of the barrier. We propose a density-barrier construction algorithm that involves the following operations: First of all, a barrier that requires the minimum total moving distance for all sensors is constructed using the least squares line approach. Through recursive elimination of outlier sensors, a new barrier can be obtained. The algorithm will find the assembly points on this barrier and the optimal assembly point for each barrier member respectively. Finally, the algorithm uses the hole-handling mechanism to achieve barrier construction and improve the network lifetime. In this paper, the variation in network lifetime under different settings of the outlier value is also analyzed through simulation experiments.